This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointer on loading ImageIcon
 
Similar Threads
about splash screen with jar file...
Store byte[] array in MS Access - Help!
SplashScreen
Putting variable text over a splash screen
Showing Images in Splash Screen