9

I was writing a small application and when I tried to create an ImageIcon I always got an exception. The exception was caused by this line of code:

prayerLevel.setIcon(new ImageIcon(getClass().getResource("/icons/icon_prayer.png")));

Now within my program, the folder /icons/ does exist. I don't know if it makes the difference but the class file is within a package, where as the icons folder is within the project folder (when you would see the bin and src folder).

I have looked around for a bit and I couldn't find a solution that could help me solve the problem. Perhaps any of you guys could help?

Edit: someone asked for my folder hierarchy:

Folder Hierarchy

I know the class file is not in the same folder as the icons are, but I've made applications where I had to load files from a different folder and doing /folder/ always used to work.

Edit 2:

System.out.println(getClass().getResource("/icons/icon_prayer.png") == null);

Prints true.

Martin Tuskevicius
  • 2,590
  • 4
  • 29
  • 46
  • 1
    how does the folder hierarchy look like? When you say /icons folder exists, where exactly is it? – posdef May 16 '11 at 20:36
  • You should probably include the relevant error message - particularly the stack trace - as that will show us where exactly the problem is occurring. – Nate W. May 16 '11 at 20:49
  • I figured out why I am getting the NullPointer, but now I cannot figure out why class.getResource( ... ) is null. The file 'icon_prayer.png' is in folder 'icons.' – Martin Tuskevicius May 16 '11 at 20:52
  • is the icons folder on the classpath when the application is run? – matt b May 16 '11 at 23:48
  • The NPE isn't from `Class.getResource()`, contrary to your title. It results from your failure to check the result. – user207421 Nov 11 '16 at 09:46

5 Answers5

13

I believe the NPE is being thrown from the ImageIcon constructor as getResource is returning null.

Try the following:

getClass().getClassLoader().getResource("/icons/icon_prayer.png")

Or:

ClassLoader.getSystemResource("/icons/icon_prayer.png")
Nate W.
  • 9,141
  • 6
  • 43
  • 65
4

As far as I know getResource() will look into locations of known resources, in other words if the folder /icons/ is not seen as a resource folder it will not as you had expected. There are two ways of going around this as far as I know:

1) Set icons folder as a resource to the application, then you can use getResource() for instance URL css_url = getClass().getResource("/resource/style.css");

For more info on this option, see http://lj4newbies.blogspot.com/2008/03/using-classgetresource-load-resource.html

2) Get the icon as a regular file without using getResource() method. This is actually adviced in Swing tutorials on Sun/Oracle own documentation .

Generally, applications provide their own set of images used as part of the application, as is the case with the images used by many of our demos. You should use the Class getResource method to obtain the path to the image. This allows the application to verify that the image is available and to provide sensible error handling if it is not. When the image is not part of the application, getResource should not be used and the ImageIcon constructor is used directly. For example:

ImageIcon icon = new ImageIcon("images/middle.gif", "a pretty but meaningless splat");

Hope this helps, good luck!

posdef
  • 6,498
  • 11
  • 46
  • 94
3

Old thread but since I bumped into a similar problem just now...

I'm using Eclipse and I copied a file to the "resources" folder using system commands (cp). However, eclipse threw a NullPointerException because I didn't refresh the "resources" folder. So the file was there but Eclipse didn't see it.

So in Eclipse: "Package Explorer" -> "resources" -> Mouse right click -> refresh. This fixed it for me.

dariober
  • 8,240
  • 3
  • 30
  • 47
2

I added my music, images, etc to a folder added to the build path. Then I just used

URL url="CurrentClass".class.getClassLoader().getResource("media file name not the path");
setIconImage(new ImageIcon(url.getPath()).getImage());

to set the image icon.

Pang
  • 9,564
  • 146
  • 81
  • 122
  • This did the trick. I had added my Resources/data folder to the class path, but I was just using class.getResource instead of class.getClassLoader().getResource. Which doesn't work if you are in the default package. – Randy Feb 25 '16 at 03:14
  • Nice article for this is here https://howtodoinjava.com/java/io/read-file-from-resources-folder/ – qwebek May 26 '21 at 13:58
-3

The only thing that can throw a NullPointerException in this line of code is the first ., which means that prayerLevel is null.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • 1
    This isn't true. The constructor `ImageIcon(URL)` will throw a NPE if the provided URL is null as the implementation is simply: `public ImageIcon(URL location) { this(location, location.toExternalForm()); }` – Nate W. May 16 '11 at 20:41
  • But then the NullPointerException is not caused (directly) by *this* line, but by some line from the constructor. – Roland Illig May 16 '11 at 20:45
  • That's a moot point. Either way, this would've been clear had the OP included the stacktrace. Note to OP: include the relevant error message! – Nate W. May 16 '11 at 20:48
  • The URL provided as an argument for the constructor is null. – Martin Tuskevicius May 16 '11 at 20:50
  • You're right, hence the NPE from the `ImageIcon` constructor. – Nate W. May 16 '11 at 21:04