Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: NullPointerException from class.getResource( ... )

Tags:

java

classpath

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.

like image 496
Martin Tuskevicius Avatar asked May 16 '11 20:05

Martin Tuskevicius


People also ask

How do I fix Java Lang NullPointerException in Java?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

Can you catch a NullPointerException?

Programmers typically catch NullPointerException under three circumstances: The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem. The program explicitly throws a NullPointerException to signal an error condition.

What does not catch NullPointerException like?

Catching NullPointerException may mask an underlying null dereference, degrade application performance, and result in code that is hard to understand and maintain. Likewise, catching RuntimeException , Exception , or Throwable may unintentionally trap other exception types and prevent them from being handled properly.

Is NullPointerException checked or unchecked?

NullPointerException is an unchecked exception and extends RuntimeException class. Hence there is no compulsion for the programmer to catch it.


4 Answers

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.

like image 82
dariober Avatar answered Oct 11 '22 08:10

dariober


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")
like image 29
Nate W. Avatar answered Oct 11 '22 07:10

Nate W.


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.

like image 21
Valencia Starr Avatar answered Oct 11 '22 06:10

Valencia Starr


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!

like image 29
posdef Avatar answered Oct 11 '22 08:10

posdef