PDA

View Full Version : problem loading image to JPanel with getResource()



badperson
June 27th, 2009, 07:26 PM
I'm trying to load an image to a Panel with the following code:


private Image image;

public LaunchScreenPanel(){
URL url = this.getClass().getResource("lib/images/gui-image.jpg");
if(url==null){
System.err.println("URL is null: ");
System.err.println(this.getClass());
System.err.println("dir: " + System.getProperty("user.dir"));
}
image = Toolkit.getDefaultToolkit().getImage(url);
}

I'm getting this output:


URL is null:
class com.jasonwardenburg.goalapp.gui.LaunchScreenPanel
dir: /media/files/code/java/Workspaces/GoalApplication
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImag eSource.java:115)
at sun.awt.image.URLImageSource.getDecoder(URLImageSo urce.java:125)
at sun.awt.image.InputStreamImageSource.doFetch(Input StreamImageSource.java:258)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher. java:189)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:1 53)\

the URL object looks like it's null...I looked around on the internet and found examples like the above...anyone know how to fix this?
The code works when I have the file loaded as a File object within eclipse. But when I run the code as a jar or through a bash file, it can't find the image (when loaded as a file instead of getResource());
thanks,
bp

froggyswamp
June 28th, 2009, 01:10 AM
You're dealing with one of those ugly situations that happen while learning one language or another.
The getResource() method returns slightly different paths for apps packed inside a .jar.
Debug yourself: let it print (show as a GUI alert) the URL when running from a .jar file and when running "as usual".
So, you'll end up creating a "helper" method that "understands" when the app is running from a jar when when not, and based on that build the correct path to your lib/image.
You're prolly gonna fiddle with class.getProtectionDomain.getSourceCode() or so, don't remember for sure.

pbrockway2
June 28th, 2009, 01:23 AM
this.getClass().getResource("lib/images/gui-image.jpg");

This will look for the image relative to whereever LaunchScreenPanel.class is located. The nice thing about this is that it will find the image (providing it is there!) regardless of whether LaunchScreenPanel.class is a file (in which case it looks in subdirectories) or an entry in a jar archive (in which case it looks in the relevent subentry).

Follow the advice given and actually print the URL. Then check that you have actually put the image in the same place you are looking for it.

For a simple deployment where the classes and the image (and other) resources are located in known locations relative to one another, the getResource() approach will be effective whether or not the classes and resources are combined into a single jar archive.

froggyswamp
June 28th, 2009, 12:36 PM
For a simple deployment where the classes and the image (and other) resources are located in known locations relative to one another, the getResource() approach will be effective whether or not the classes and resources are combined into a single jar archive.
My bad, I confused that with detecting at runtime the dir the app is located in.

badperson
June 29th, 2009, 12:49 AM
so the code I posted may have worked had I executed it from a jar as opposed to within eclipse?

The image also failed to display when I executed the code off the command line using the relative path.
bp

pbrockway2
June 29th, 2009, 05:04 AM
So where in the LaunchScreenPanel constructor are you looking for the image? Ie what is printed with


System.out.println(this.getClass().getResource(""));


lib/images/etc is being resolved relative to that location. And the image (whether it be a file or a jar entry) must be at that place or you will get a null URL.

badperson
June 29th, 2009, 10:10 PM
thanks,
that code you suggested returns the following:

in constructor: file:/media/files/code/java/Workspaces/GoalApplication/bin/com/jasonwardenburg/goalapp/gui/

I think it has something to do with the fact that eclipse creates the bin and src folders.

I tried modifying the code you suggested to this:

this.getClass().getResource("../../../../")

which returns


file:/media/files/code/java/Workspaces/GoalApplication/bin/

but if I try to go up again to get at the parent dir, I get null.
Maybe I need to set this up in ant?
Or can I import the image folder into eclipse?
bp

badperson
June 30th, 2009, 08:15 PM
fixed it...I needed to move the folder that held the images under the "src" folder in eclipse. Then the getResource() method worked.

pbrockway2
June 30th, 2009, 10:20 PM
I'm glad you've got it working.

You're right - Eclipse seems to have been set to compile (or copy) the contents of the src folder to the bin folder. This bin folder is used as the classpath, so that you can't really go "above" it using getResource().