13

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.

3
  • 2
    Did Eclipse copy the image properly into your build output folder? Where is the class calling getResource? Commented Aug 26, 2011 at 2:44
  • In Eclipse, open the Navigator view. Open the /bin folder of the project. Does it have the /resources/01.png file? Commented Aug 26, 2011 at 2:47
  • @Michael: Thanks for pointing that out, i just did a clean and rebuilt. Works now Commented Aug 26, 2011 at 2:47

5 Answers 5

15

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

Sign up to request clarification or add additional context in comments.

3 Comments

ECLIPSE ...... i have the same issue today even clean build doesn't solves it . Ah
No, it's not only eclipse. IntelliJ also has the same problem
Netbeans 12.X . resolved the error
5

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.

Comments

0
  1. make sure the resource file is under your class path. For example, check whether your module/project is compiled to xxx.jar and saved under folder "target"
  2. I had this problem as well, when I run mvn install, there is NO jar file generated, that's why no resource found in jar file in target
  3. The cause of no jar generated is caused by following config in pom.xml, remove it and rerun install would be fine.

    <packaging>pom</packaging>
    

Comments

-1

If using JDeveloper, make sure that you have the extension of the file that you want copied to the classes folder for use selected. Right click on the project and choose project properties to get to this screen.

JDeveloper

Comments

-2

I would guess the URL should be "01.png" not "/resources/01.png"

2 Comments

This did not work. I am seeing that the URL is created but the value is still null after execution.
Not really, resources beginning with "/" are treated as absolute paths within the classpath: download.oracle.com/javase/6/docs/api/java/lang/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.