Advertisement

NullPointerException Problem

Started by December 30, 2014 02:45 PM
5 comments, last by Glass_Knife 10 years, 1 month ago

I get this warning... If you could plz help...

Exception in thread "main" java.lang.NullPointerException
at DataBase.<init>(DataBase.java:17)

import java.io.*;
import java.util.ArrayList;

public class DataBase {
	private ArrayList<String> words = new ArrayList<String>();
	public DataBase(){
		try{
			String line = " ";
			FileReader freader = new FileReader("someText.txt");
			BufferedReader reader = new BufferedReader(freader);
			File fileOut = new File("newText.txt");
			FileWriter writer = new FileWriter(fileOut);
			while(line != null)
			{
				String[] parts = null;
				line = reader.readLine();
				parts = line.split(" ");
				for (String word : parts) 
				{
					word = word.toUpperCase();
					word = word.replaceAll("[:»«,.;!?(){}\\[\\]<>%]","");
					if(word.length() > 3 && !word.matches(".*\\d+.*"))
					{
						writer.write(word);
						writer.write(System.lineSeparator());
						words.add(word);
					}
				}
			}
			reader.close();
			writer.close();
		}
			catch(FileNotFoundException e)
			{
				System.out.println("File not found!");
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
	}

}

Thanks in advance :)

Failure is not an option...

My guess is that the previous line, 16, returned a NULL pointer.
line = reader.getLine();
Advertisement

Hello shadowstep00,

I assume reader.readLine() returns null. Maybe the file does not exist or there are no more lines to read.

Regards

Henry

The file exists and it gets read. But it stops at the end somehow wrong.

So the problem propably is that there are no more line left to read???

Isn't while(line != null) preventing this from happening?

EDIT:

I added:

if(line == null)

break;
and now I dont get the warning anymore :) thanks!

Failure is not an option...

while(line != null) is only checked once at the beginning of each loop-iteration. You have it in line 13, so if you then change "line" in line 16 like you did:


line = reader.readLine();

it will only break the loop after the following code has been executed, so you have to manually add an additional check like you did.

I always write a loop like that as:


        String line = reader.readLine();
        while (null != line) {
            // Do you thing here
            line = reader.readLine();
        }

So preventing extra checks

Advertisement

I always write a loop like that as:


        String line = reader.readLine();
        while (null != line) {
            // Do you thing here
            line = reader.readLine();
        }

So preventing extra checks

I like to get even more fancy:



String line = null;
while( (line = buf.readLine()) != null ) {

   // do stuff here
}

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

This topic is closed to new replies.

Advertisement