This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointer on loading ImageIcon

 
Richard Cook
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I wonder if you can help me. I am trying to load an image (PNG) into an ImageIcon.

But all I get is a NullPointerException wherever I place the image.

My code:


How I call it:


I know the file exists and I have tried it with and without the preceding '/'.

Help would be greatly appreciated as this is currently holding me up.

Thanks


Richard
 
Jim Akmer
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try myClass.class.getResource(relativePath/img);

where myClass is the class name in your project and the relative path is the directory in your project you have strored the img
 
Richard Cook
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ya,

Thanks for that, but I'm afraid that didn't work.

What I can't understand is that it is a form created by netbeans and I also have a label with an image which works.

And the code for that is:

Which was generated, but when I copy the code to my section it fails.

Weird!

Richard
 
Jim Akmer
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use getClass(). In the class you are trying to use the code, use the class name e.g of the class you copied (className.class.getResource) that snippet from, and works for that class. Make sure that the directory has the img you want.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic