GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes NullPointer on loading ImageIcon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "NullPointer on loading ImageIcon" Watch "NullPointer on loading ImageIcon" New topic
Author

NullPointer on loading ImageIcon

Richard Cook
Greenhorn

Joined: Aug 18, 2010
Posts: 22
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

Joined: Jul 06, 2010
Posts: 104
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

Joined: Aug 18, 2010
Posts: 22
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

Joined: Jul 06, 2010
Posts: 104
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.
 
GeeCON Prague 2014
 
subject: NullPointer on loading ImageIcon