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.

Recommended Answers

All 6 Replies

'tmp' string might be null. put a check like this after you do the 'readLine' and try

if(tmp != null)
{
//your code
}
commented: thanks +2

The documentation for BufferedReader states the following ..."Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached." When you reach the end of the file, the value is "null".

http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine()

As Agni said, tmp is null which is why you are getting the NullPointerException. It happens because you try to do the following when tmp = null:

int letters = tmp.length();

You can use an "if" statement as Agni mentioned or just combine lines 31 and 32 as follows:

while ((tmp=br.readLine()) != null){

Hi, thanks to both. That solved the problem although I still don't quite understand.
Cgeier, the thing is, the NullPointerException doesn't come when it's finished reading the original file. It comes in the middle of the file and then continues reading until the end.

upload the data files if they are not too big, we'll see if we can help.

thank you,
here is the file

The null is because it's reading past the last line of the file. So when it has read the last line the 'while' is true, then it reads the next line(one after last one) then when you access the length of tmp you get a nullpointer exception. re-write your loop to prevent that.

As a future strategy, try to put a lot of prints and you might be able to figure out such bugs very quickly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.