Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getClass().getResource() always returning null

I am trying to set the URL of an image that is located in my resource folder in my src folder. It is always setting the URL to null. The image is there and everything is named correctly. What is the issue? Thanks!

My file structure:

  • sp1
    • src
      • resources
        • 01.png

Here is my code where I am trying to set the URL for the picture:

this.setImagePath(getClass().getResource("/resources/01.png"));

And the setImagePath is doing this, I am sure this is not the problem, because I followed the execution and it is setting it equal to null.

public final void setImagePath(URL imagePath) {
    this.imagePath = imagePath;
}

Solution

I had to clean the build and rebuild. It worked after that fix.

like image 233
prolink007 Avatar asked Aug 26 '11 02:08

prolink007


2 Answers

I had to clean the build and then rebuild. Works now.

like image 183
prolink007 Avatar answered Oct 01 '22 13:10

prolink007


You need to check that, however you're building, the image file gets copied over to wherever the class files are compiled to. I.e. if your class is com.foo.MyImageLoader and located at <build dir>/com/foo/MyImageLoader.class, then the image should be at <build dir>/resources/01.png for you to be able to load it like that.

like image 34
Ryan Stewart Avatar answered Oct 01 '22 14:10

Ryan Stewart