• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nullpointer exception reading file?  RSS feed

 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'm stuck and have no idea why it's giving me a null pointer exception while trying to read the file? Any help would be much appreciated. I have the number.txt file under src/package/ Thanks.
 
Paul Clapham
Sheriff
Posts: 22508
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should start by looking at the stack trace -- that's the blob of text which told you that you had a NullPointerException. It should provide useful information such as the line of code which threw the exception and the method and class in which that line is located.

If that isn't enough help, then by all means post the stack trace here, or at least tell us what line of code has the problem.
 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You should start by looking at the stack trace -- that's the blob of text which told you that you had a NullPointerException. It should provide useful information such as the line of code which threw the exception and the method and class in which that line is located.

If that isn't enough help, then by all means post the stack trace here, or at least tell us what line of code has the problem.


It's line 26 that's giving me the problem.
 
Paul Clapham
Sheriff
Posts: 22508
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it's calling the getResource() method which specifically causes the exception?



So that means that there isn't any resource named "numbers.txt" in the same directory as your Buff class anywhere in your classpath. There could be a variety of things causing that -- perhaps it's not in the classpath at all, or perhaps it's in some other folder. So review your classpath and see what's up with that.
 
Paul Clapham
Sheriff
Posts: 22508
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually "in the same directory as your Buff class" isn't quite right, since you used the version of getResource() from ClassLoader and not from Class. Probably it expects to find it in the root of one of the classpath nodes, although the documentation is kind of opaque and I've never used that version before.

Also it returns a URL, and the getFile() method of a URL isn't necessarily going to return a file-system path which makes sense to the new File() constructor you used. You're better of to use getResourceAsStream() instead, and use the InputStream it returns directly instead of hoping for a file.
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't main just be

or maybe "src/package/numbers.txt"?
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you're not closing the resource, you should try this to avoid a memory leak:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!