1

Program to read file and print all the characters that are letters, throws a NullPointerException when it gets to the last line.

import java.io.*;

public class Foo {

    public static void main(String[] args) throws IOException {

        FileReader file = new FileReader(new File("source.txt"));

        BufferedReader read = new BufferedReader(file);

        String line = read.readLine();

        while (line != null) {
            for (int i = 0; i < line.length(); i++) {
                line = read.readLine(); // this is where the problem is. When it reaches the last line, line = null and the while loop should terminate!
                if (Character.isLetter(line.charAt(i))) {
                    System.out.print(line.charAt(i));
                }
            }
        }
    }

}
Jack
  • 497
  • 3
  • 10
  • 22
  • You want to look into the javadoc for the BufferedReader you are using. See here: http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html Self help is the best help. Also note this is a duplicate of: http://stackoverflow.com/questions/16265693/how-to-use-buffered-reader-in-java The answer here will be the same. – ThePerson Feb 24 '15 at 20:19
  • 1
    Why do you try and read as many lines as the line's length? That doesn't really make sense – fge Feb 24 '15 at 20:20
  • While loops only check the condition(s) at the **start** of each iteration. They will not terminate mid loop just because the condition will be false at the start of the next iteration. – chancea Feb 24 '15 at 20:26
  • @fge This was supposed to be a minimum working example of a bigger problem. I've updated the code so that it iterates through each character and prints it if it's a letter. – Jack Feb 24 '15 at 20:35

4 Answers4

1

Just realize you can do like this:

   String line = null;

    while ((line = read.readLine()) != null) {
        for(int i=0; i<line.length(); ++i)
        {
          if(Character.isLetter(line.charAt(i)))
             System.out.println(line.charAt(i));
        }
    }

Do not forget to close streams, and it would be better to encapsulate everything in try block.

Piotr Zych
  • 483
  • 4
  • 19
  • Zach This was supposed to be a minimum working example of a bigger problem. But I got that wrong. :( I've updated the code so that it iterates through each character and prints it if it's a letter. – Jack Feb 24 '15 at 20:42
1

While loops do not work like how you explained them in your comment:

// this is where the problem is. When it reaches the last line, line = null and the while loop should terminate!

While loops only check the condition(s) at the start of each iteration. They will not terminate mid loop just because the condition will be false at the start of the next iteration.

So this null check you have at the start while (line != null) will only and always happen at the start of each iteration, even if line was set to null mid iteration

So as others have shown you can either construct your while loop as:

String line = null;

while ((line = read.readLine()) != null) 
{
    for (int i = 0; i < line.length(); i++) 
    {
        if (Character.isLetter(line.charAt(i))) 
        {
            System.out.print(line.charAt(i));
        }
    }
}

and remove all other read.readLine() statements from your code. (which is the shortest lines of code).

Or if you want to be more explicit for perhaps more readability, you can keep the initial read.readLine() as you have it but move the iterative read.readLine() to after all your uses for line are done:

String line = read.readLine();

while (line != null) 
{
    for (int i = 0; i < line.length(); i++) 
    {
        if (Character.isLetter(line.charAt(i))) 
        {
            System.out.print(line.charAt(i));
        }
    }
    line = read.readLine();
    //line is never used after this so an NPE is not possible
}
chancea
  • 5,858
  • 3
  • 29
  • 39
0

Your for (int i = 0; i < line.length(); i++) does not make sens here. The length of a line has nothing to do with the number of lines in the file.

Change your code to :

String line = null;
while ((line = readLine()) != null) {
    System.out.println(line.length());
    // do what ever you need with line
}
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
0

Try while((line = read.readLine()) != null)

This initializes the value as its checked against the while condition at every loop iteration.

James
  • 903
  • 7
  • 22