I'd appreciate if somebody could have a look at this code and tell me where the problem is.
The program is meant to take a .txt file and split it in three different files: one with words of 1 to 4 letters, one with words of 5 to 7 and the last one with words of 8 letters onwards.
For some reason the program starts and does well (that is, behaves as expected) until it reaches about the middle of the original file. Then I get a NullPointerException: line 34.
Two of the destination files are OK and one is truncated.
Here is the code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class DictSplitter {
File dict = new File("diccionario.txt");
File easy = new File("easy.txt");
File diff = new File("difficult.txt");
File noWay = new File("xtreme.txt");
public static void main(String[] args) throws IOException {
DictSplitter ds = new DictSplitter();
BufferedReader br = new BufferedReader(new FileReader(ds.dict));
BufferedWriter bwE = new BufferedWriter(new FileWriter(ds.easy));
bwE.write("Word length: up to 4 letters");
bwE.newLine();
BufferedWriter bwD = new BufferedWriter(new FileWriter(ds.diff));
bwD.write("Word length: 5 to 7 letters");
bwD.newLine();
BufferedWriter bwNW = new BufferedWriter(new FileWriter(ds.noWay));
bwNW.write("Word length: from 8 letters onwards");
String tmp = "";
int easyCount = 0, diffCount = 0, nwCount = 0;
while (tmp != null) {
tmp = br.readLine();
// System.out.println("tmp: " + tmp);
int letters = tmp.length();
switch (letters) {
case 0:
case 1:
case 2:
case 3:
case 4:
bwE.write(tmp);
bwE.newLine();
easyCount++;
break;
case 5:
case 6:
case 7:
bwD.write(tmp);
bwD.newLine();
diffCount++;
break;
default:
bwNW.write(tmp);
bwNW.newLine();
nwCount++;
break;
}
}
bwE.close();
bwD.close();
bwNW.close();
System.out.println("easy: " + easyCount + "\t\tdifficult: " +
diffCount + "\t\tnoWay: " + nwCount);
}
}
Thank you in advance.