Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException problems  RSS feed

 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a program in which I am using file I/O. I want to read to the end of the file. In the code snipped below I have read a line and am checking for null.



As you can see I am printing the line before I check for null. It prints this:

but gives me the following NullPointerException when checking for null:

NullPointerException:
at MosBrandi.load(MosBrandi.java:238)
at MosBrandi.terminate(MosBrandi.java:169)
at MosBrandi.mos(MosBrandi.java:33)
at MosBrandi.executeUserProgram(MosBrandi.java:383)
at MosBrandi.startExecution(MosBrandi.java:288)
at MosBrandi.load(MosBrandi.java:270)
at MosBrandi.terminate(MosBrandi.java:169)
at MosBrandi.mos(MosBrandi.java:33)
at MosBrandi.executeUserProgram(MosBrandi.java:383)
at MosBrandi.startExecution(MosBrandi.java:288)
at MosBrandi.load(MosBrandi.java:217)
at MosBrandi.main(MosBrandi.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)


Line 238 in my code is the if statement, which I've bolded.

I don't know why on earth this would be giving me a null pointer exception when I am checking for null. I'm very confused and any help in figuring out my conundrum would be very much apprecaited. Thank you!
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To compare an object to null you must do it like this:The way you did it, you try to call the equals() method of null, and that's why the exception is thrown.
 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As is usual it was something simple that I missed. Thanks for your help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!