troubleshooting Question

NetBeans IDE: Classpath not working for a JLabel icon image file

Avatar of mgalig1010
mgalig1010 asked on
10 Comments3 Solutions2834 ViewsLast Modified:
I get an error after using NetBeans to select an image file for a JLabel icon.

IDE code that works:
gallows.setIcon(new javax.swing.ImageIcon("K:\\Hangman\\images\\gallows.gif"));

IDE code that fails when I run the project:
gallows.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gallows.gif")));

How I'm selecting the image location:
1) from the IDE click the [...] next to the icon property of my label then choose classpath and click [select file].
2) I am able to navigate to the file correctly and it is displayed in the preview window. I can even see it when I click on the test form button.

If I run my file I get an error:
netbeans Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(
        at Keyboard.initComponents(
        at Keyboard.<init>(
        <following errors removed by me>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 3 Answers and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros