title: Reading file line by line- terminating while loop after reaching last line
body: <p>Program to read file and print all the characters that are letters, throws a NullPointerException when it gets to the last line.</p>
  
  <pre><code>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 &lt; 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));
                  }
              }
          }
      }
  
  }
  </code></pre>