wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes ImageIcon NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ImageIcon NullPointerException" Watch "ImageIcon NullPointerException" New topic
Author

ImageIcon NullPointerException

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
I've got a program that I developed in Eclipse, which runs great. However, when I export it to a jar file(all resources included), it keeps giving me an error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(Unknown Source)

I checked the jar file to make sure the image is in there, and it is.
Any suggestions? I'm stumped. Here's the code causing the error:



ApplicationConstants.cancel is a static variable - see below:


Thanks!
[ February 20, 2006: Message edited by: Jennifer Sohl ]
vinodkumar kolakampadam
Greenhorn

Joined: Feb 20, 2006
Posts: 2
Hi jeni

Modify your code
from
cancel = new JCheckBox("Cancel PO",new ImageIcon(getClass().getResource(ApplicationConstants.cancel)));


to

cancel = new JCheckBox("Cancel PO",new ImageIcon(getClass().getClassLoader().getResource(ApplicationConstants.cancel)));

you have to the get the classloader to load the images
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Thanks for the response! I haven't tried that yet, but did find something interesting (and weird). I tried all of my other images that I had in Eclipse and they all worked with the code that I currently have. I could not think of anything else, so just for craps and giggles, I renamed the image from cancel.jpg to redx.jpg, then exported it to a jar file and it worked! Not sure why, though. That'll be something to ponder later this week.
Thanks again!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ImageIcon NullPointerException