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")));
label.setIcon(new ImageIcon("URL/copied/from/directory.jpg"));
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.label.setIcon(new ImageIcon(getClass().getResource("URL/copied/from/directory.jpg")));
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.getClass().getResource.read("URL/copied/from/directory.jpg");
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).