Search code examples
javaimageswingembedded-resource

getClass().getResource(..) makes image icon suddenly null


I have been reading a lot of questions related to this problem on this website. But somehow, none of the suggested answers worked.

I want to use images in my java program. I build the .java file today for the first time, but the images won't apply to the finished build. I tried a lot. getClass.getResource(), the Class loader, reassigning my resource folder, moving the images, switching the images, different types of URLs, but I run into one of 2 problems either way:

I either get the above problem, when my code for the line is:

label.setIcon(new ImageIcon("URL/copied/from/directory.jpg"));

or, when I do something like this:

label.setIcon(new ImageIcon(getClass().getResource("URL/copied/from/directory.jpg")));

Then I get a warning, which states this URL might be null.

when I use getClass().getResource.read("URL/copied/from/directory.jpg"); I actually get a compiling error with the same issue.

I am really not sure what is wrong with the code or the URL. One problem I have with the code examples in other similar questions is that I cannot tell which part of the code is syntax and which is not.

As requested here is the original code:

climateImageLabel.setIcon(new ImageIcon(getClass().getResource("src/DSA_Wildnistool/Pictures/KEIN BILD.jpg")));

Solution

    1. label.setIcon(new ImageIcon("URL/copied/from/directory.jpg"));
      This could not work, as the String should represent a File rather than a URL, but important point here, don't share with us 'something like' the code or String being used. Share the actual copy/pasted text.
    2. label.setIcon(new ImageIcon(getClass().getResource("URL/copied/from/directory.jpg")));
      This tries to do too much in one line. Break it down. Use getResource(..) to get an URL (print out the URL). Use the URL to form an Icon, check it's not null. Use the icon to create the label.
    3. getClass().getResource.read("URL/copied/from/directory.jpg");
      Of course this would not compile, but the statement "One problem I have with the code examples in other similar questions is that I cannot tell which part of the code is syntax and which is not." is especially telling. It is .. Java 101 to be able to read a compiler error and either solve it or form a specific question about solving it. OTOH creating a cross-platform GUI using embedded resources is somewhat more advanced than Java 101. Perhaps you should set this project aside for a while and practice Java basics until you are familiar with them.

    If you would like to try and continue with solving this problem, please post a short but complete code example based on the 2nd approach (the only one which made sense).

    But to provide help, we'll need to know at least the package name of the class in which this code is called from (the 'short but complete example' will reveal that), and the directory structure of the project itself (at least showing the paths to the class and the image).