0

I am reading lines from a file using BufferedReader. The code with the issue is:

while((line.charAt(0)!='X') && (line !=null))

This gives a `NullPointerException when it reaches the last line. However:

while(line !=null)

alone works perfectly i.e. BufferedReader doesn't catch an exception. So, the problem seems to be when implementing multiple conditions with a BufferedReader condition.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 3
    check for null BEFORE you check the first char. – Stultuske Dec 03 '15 at 09:57
  • "This gives a `NullPointerException` when it reaches the last line." No it doesn't. It gives a `NullPointerException` *after* the last line. Clearly you aren't checking correctly for end of stream. "i.e. `BufferedReader` doesn't catch an exception": `BufferedReader` is not involved in this exception. *Your code* is involved in this exception. "So, the problem seems to be when implementing multiple conditions with a `BufferedReader` condition" : rubbish. The problem is that you aren't checking the result of `BufferedReader.readLine()` for null. " – user207421 Dec 03 '15 at 10:05

0 Answers0