0

txt file that I am reading into my program line by line. My goal is to display this text with no punctuations and in all lowercase. So far I am able to display this text in this way with the code below however I keep receiving a run-time error of a nullpointer exception at the last line of my "while" expression and I am not sure where my error is. I believe the problem may have been in my boolean while expression and I tried changing input != null to reader.readLine() != null with no success. Thank you!

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class BinaryTree {

    public static void main(String[] args) {

        String inputFile = "pg345.txt";
        FileReader fileReader = null;

        try {
            fileReader = new FileReader(inputFile);
        }   catch (FileNotFoundException e) {
                e.printStackTrace();
        }

        BufferedReader reader = new BufferedReader(fileReader);
        String input;

        try {
            input = reader.readLine().toLowerCase().replaceAll("[^a-zA-Z ]", "").toLowerCase();
            System.out.println(input);
            while (input != null) {


                input = reader.readLine().toLowerCase().replaceAll("[^a-zA-Z ]", "").toLowerCase();
                System.out.println(input);
            }


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            fileReader.close();
        }   catch (IOException e) {
            e.printStackTrace();
        }





    }


}
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
jkwon
  • 13
  • 3
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Michael Lloyd Lee mlk Dec 02 '14 at 17:19

1 Answers1

1

Your culprit is

reader.readLine().toLowerCase().replaceAll("[^a-zA-Z ]", "").toLowerCase()
reader.readLine().toLowerCase().replaceAll("[^a-zA-Z ]", "").toLowerCase();

Here the invocation

reader.readLine()

might return null if there are no more lines in the file.

MJSG
  • 1,035
  • 8
  • 12