It's not a secret anymore!
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

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:

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

Joined: Feb 20, 2006
Posts: 2
Hi jeni

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


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!
I agree. Here's the link:
subject: ImageIcon NullPointerException
It's not a secret anymore!