Why do i get a NullPointerException at If statement in do while loop when reading a simple textfile in Java

百般思念 提交于 2019-11-30 21:41:14

问题


I'm new in Java, I have a lot of work with text and I've got an idea to make a simple program to do my work. I'm getting error Exception in thread "main" java.lang.NullPointerException at com.text.work.Main.main(Main.java:25)

public class Main {

public static void main(String[] args) throws IOException {
    int someNumber = 0;
    PrintWriter saveFileOne = new PrintWriter("save.txt");
    PrintWriter saveFileTwo = new PrintWriter("otherThings.txt");
    FileReader fileReader = new FileReader("read.txt");
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String word = bufferedReader.readLine();


    do {
        word = bufferedReader.readLine();

        if (word.toString().equals("true")) { //this is line 25
            saveFileOne.println("No: " + ++someNumber + " = " + word);
        } else {
            saveFileTwo.println("Yes: " + someNumber + " = " + word);
        }

    } while (word != null);
    saveFileOne.close();
    saveFileTwo.close();
    bufferedReader.close();
    System.out.println("Done!");
}

}

回答1:


I've the same problem today, two addidional brackets is the answer

(word = bufferedReader.readLine()) != null)




回答2:


From the BufferedReader#readLine() documentation:

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

This means you've obviously reached the end of the stream.

If I were you, I would change the loop with a while one.

while ((word = buffer.readLine()) != null) {
 ..
}



回答3:


Obviously word is null because bufferedReader.readLine() encountered the end of the stream and returned null.

You should do the check against null before entering the while body

while(null != (word = bufferedReader.readLine())) {
}

and to avoid such NPEs in general while comparing with constants call equals this way:

"true".equals(other string) // "true" is never null



回答4:


Make sure that word is not null. Change the code as following code snippet.

 do {
       word = bufferedReader.readLine();
       if(word != null) { //If word is null, no need to go in if block
         if (word.toString().equals("true")) {
            saveFileOne.println("No: " + ++someNumber + " = " + word);
         } else {
            saveFileTwo.println("Yes: " + someNumber + " = " + word);
         }
       }

    } while (word != null);

You can also change your loop for reading a file can done easily using following code

   if(buffer != null) {
        while ((word = buffer.readLine()) != null) {
           if (word.toString().equals("true")) {
               saveFileOne.println("No: " + ++someNumber + " = " + word);
           } else {
               saveFileTwo.println("Yes: " + someNumber + " = " + word);
           }
        }
    }


来源:https://stackoverflow.com/questions/18949378/why-do-i-get-a-nullpointerexception-at-if-statement-in-do-while-loop-when-readin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!