0

I am trying to get an image from the resource folder of my java application. However, no matter in which way I try to get the resource, it gives me a NPE.

This is my code:

JLabel lbl_pic;
ImageIcon i = new ImageIcon(getClass().getClassLoader().getResource("res/img/user.png")); //NPE occurres here
lbl_pic = new JLabel(i);

My NetBeans project folder looks like this:

ProjectName
    >Source Packages
        >com.scfa.projectName
            >main.java
        >com.scfa.projectName.res.img
            >user.png

Can somebody please tell me, what I have done wrong?

I already looked at the following topics, but couldn't get it working:

Thanks a lot for your help i advance :)

Community
  • 1
  • 1
Fabian Schneider
  • 799
  • 1
  • 13
  • 40
  • `getResource("/res/img/user.png")` – rkosegi Feb 24 '17 at 06:59
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – rkosegi Feb 24 '17 at 06:59
  • @rkosegi: I tried that and it didn't work... I know what a NPE is; and normally how to fix it, but I cant get it working this time. The image is in the folder I also tried putting it in another folder and tried it that way but it didn't work either – Fabian Schneider Feb 24 '17 at 07:04
  • Shouldn't your res folder be named as resources and should be at the same level as the source packages. or have you configured your build tool to read from the new path? check the created jar file if the image is there or not. – Nithish Thomas Feb 24 '17 at 07:05
  • @NithishThomas: I just tried renaming and moving the folder to this location, but it didn't work out. the image should be there, after checking.... – Fabian Schneider Feb 24 '17 at 07:15
  • What would be the coorect, standard way to add an image to the projects resources? (perhaps I screwed up there) – Fabian Schneider Feb 24 '17 at 07:16
  • I think you are on the money with the question [how to correctly get an image from resources folder in netbeans](http://stackoverflow.com/q/6845231/7421645). I use IntelliJ and your code also gave me a `NullPointerException` until I moved the directory containing it a directory marked as a resources directory. – James Feb 24 '17 at 07:17
  • 1
    `new ImageIcon(getClass().getClassLoader().getResource("res/img/user.png"))` worked for me... but only after moving `user.png` to: `src/main/resource/res/img/user.png`. My test class was in the directory `src/main/java` – James Feb 24 '17 at 07:19
  • okay thanks that was it. solved my problem :) do you want to write an answer or should I? – Fabian Schneider Feb 24 '17 at 07:23

1 Answers1

2

You seem to be saying that the user.png is in the package com.scfa.projectName.res.img

In that case, you can get its URL using:

URL url = getClass().getClassLoader().getResource(
    "/com/scfa/projectName/res/img/u‌​ser.png");  // absolute path

or

URL url = getClass().getResource("res/img/user.png"); // relative path

In general, if you want to resolve a resource path relative to a class, you must call getResource on the Class object ... not on its classloader. The getResource method for a ClassLoader object will treat a relative resource path as if it was an absolute resource path. The same applies to the getResourceAsStream methods on Class and ClassLoader.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • There is further explanation on the difference between `getResource` on the class and class loader in this answer: http://stackoverflow.com/a/676273/7421645 though it specifically applies to the `getResourceAsStream` methods. – James Feb 24 '17 at 08:43