• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Nullpointer exception reading file?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 27468
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27468
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27468
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couldn't main just be

or maybe "src/package/numbers.txt"?
 
Knute Snortum
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you're not closing the resource, you should try this to avoid a memory leak:
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic