I have 5 image files that I wish to incorporate or embedd inside a program. I am using netbeans and I have created a directory in the files table just under the database directory (I have embedded databases) with the 5 files inside them. For good measure, I have also placed copies of the directory (and files) in the dist and src directories all named 'Images'.

I am trying to access them and use them as an image icon as a default image at start-up. User has the option later to change them.
this is what I was trying....

 CHARACTER_PHOTO.setIcon(new ImageIcon(getClass().getResource("Images/dragons.jpg")));

This is producing a runtime error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Could someone offer suggestions.....

Finding files froma Java program is always a difficult area, especially when moving from an IDE to a runtime environment. There's always confusion and doubt about where the files should be.
The getResource is a good approach. If you change your path to "/Images/dragons.jpg" and place the Images directory in the classpath then all should be OK. Most people just place the Images directory alongside the package directories for the application's class files. That way if Java can find the classes then it can find the images as well. In prticular this allows you to place everything in the same jar, and the code runs just like it did in the IDE

Evidently, I have placed the directory in the wrong location as I am still having the error. Where should I place this directory or how do I find out what the current class path is, or do I need to modify the classpath so that my directory is visible and how do I do that?

Edited 3 Years Ago by mikewyatt

Most people just place the Images directory alongside the package directories for the application's class files. Eg if you have MyMainClass in a package myApp then the Images directory would be placed next to the myApp directory (in which the MyMainClass.class file is found).

This question has already been answered. Start a new discussion instead.