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

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>(ImageIcon.java:138)
        at Keyboard.initComponents(Keyboard.java:175)
        at Keyboard.<init>(Keyboard.java:15)
        <following errors removed by me>
LVL 6
mgalig1010Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
try:

gallows.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/gallows.gif")));

What directory and package is your class in?
mgalig1010Author Commented:
I tried your suggestion and I got the same error.

My class is in the default package, in the hangman\src directory. The image is in the hangman\images directory.

Both directories are in the list of Source package folders (visible when I display the properties for my project).

This is very confusing, especially since the IDE finds the image without a problem. I only get the error at runtime.
Tommy BraasCommented:
Add the images directory to your classpath, and load the image (without the leading slash) with:

gallows.setIcon(new javax.swing.ImageIcon(getClass().getResource("gallows.gif")));
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Mick BarryJava DeveloperCommented:
> This is very confusing, especially since the IDE finds the image without a problem. I only get the error at runtime.

probably due to different classpaths, getResource() locates the resource via the classloader so you need to have the resources available from the classpath
mgalig1010Author Commented:
How do I make the resources available from the classpath?

How do I find out what my current classpath is?

How do I add a new directory to my classpath? I would need to have a different classpath for each project.
Tommy BraasCommented:
>> How do I make the resources available from the classpath?
You add the containing directory structure to the classpath

>> How do I find out what my current classpath is?
In NetBeans:
1. Right-click on your project.
2. Select Properties.
3. Click on Libraries in the dialog box which appeared in previous step
4. Click on Manage Platforms to view classpath

>> How do I add a new directory to my classpath? I would need to have a different classpath for each project.
In NetBeans:
1. Right-click on your project.
2. Select Properties.
3. Click on Libraries in the dialog box which appeared in previous step
4. Add projects, libraries and folders (directories) here as you wish

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mgalig1010Author Commented:
orangehead911,
> 3. Click on Libraries in the dialog box which appeared in previous step
> 4. Add projects, libraries and folders (directories) here as you wish
 Wouldn't this affect every project I create from then on? I see paths here to things that the JDK needs to function. I'm wanting to be able to access resaources I create. Do you have to do this on your machine to get an image to show on a JLabel?

> 1. Right-click on your project.
> 2. Select Properties.
 Why wouldn't I add my images directory to the Source package folders.  This is after all what made my image visible to the IDE when I tried to pull the image from the classpath (when modifying the icon property for a JLabel using the IDE). As far as I know this is how Java finds my source (.java) file and that is working correctly.

Please be patient as I'm new to Java and very new to NetBeans.

Marcos
Tommy BraasCommented:
>>  Wouldn't this affect every project I create from then on?
No

>> Do you have to do this on your machine to get an image to show on a JLabel?
For resources, you either put them in a location which is already on the classpath, or you have to add that location to the classpath.

>> Why wouldn't I add my images directory to the Source package folders.
Your source file directories are NOT on the classpath
mgalig1010Author Commented:
Thank You!

I have images.  This continues to work even if I move my project to a different drive. Just what I needed.

I'm still a bit confused as to why the IDE was not consistent with the runtime environment. In my (obviously expert ;)) opinion the IDE should have known the image wouldn't be available at run time and said something about it when I added it at write time.
Tommy BraasCommented:
You might be able to set NB up to copy resources from the source directory to the output directory (Eclipse does that by default), that way you would always have all your resources on the classpath.

;-D
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.