Image Problems...

I am using the following code to try and display an image on a JButton, but the URL is null for some reason...

ImageIcon explosionIcon;

URL url = getClass().getClassLoader().getResource("ExplosionIcon.GIF");

System.out.println(url);

try

{

explosionIcon = new ImageIcon(ImageIO.read(new File(url.toString())));

}

catch(IOException e)

{

System.err.println("Error reading ExplosionIcon.gif");

e.printStackTrace();

return;

}

The image file is in the same file as both the .java and the .class file. Any suggestions?

[602 byte] By [Rob_Ha] at [2007-11-26 17:02:15]
# 1
Maybe you got the case wrong.Doesn't ImageIcon have a constructor that takes a URL argument?
paulcwa at 2007-7-8 23:30:01 > top of Java-index,Java Essentials,Java Programming...
# 2
> Maybe you got the case wrong.I'm 100% positive that I have not.> Doesn't ImageIcon have a constructor that takes a URL> argument?Err... yes it does! But that did not fix the error...
Rob_Ha at 2007-7-8 23:30:01 > top of Java-index,Java Essentials,Java Programming...
# 3
Are you using an IDE that moves files around when it builds your project?
DrLaszloJamfa at 2007-7-8 23:30:01 > top of Java-index,Java Essentials,Java Programming...
# 4
Just as a test, you can try getting a URL to the .class file itself, and see if it's null.This is one of those grabbing-at-straws tests, but sometimes they help.
paulcwa at 2007-7-8 23:30:01 > top of Java-index,Java Essentials,Java Programming...
# 5
> Are you using an IDE that moves files around when it> builds your project?I'm using Eclipse. I don't know if it does that or not.
Rob_Ha at 2007-7-8 23:30:01 > top of Java-index,Java Essentials,Java Programming...
# 6
> Just as a test, you can try getting a URL to the> .class file itself, and see if it's null.Hardcoding the URL does not seem to work either...
Rob_Ha at 2007-7-8 23:30:01 > top of Java-index,Java Essentials,Java Programming...
# 7
Any suggestions?
Rob_Ha at 2007-7-8 23:30:01 > top of Java-index,Java Essentials,Java Programming...
# 8
[url http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html]How to Use Icons[/url].Dump the IDE and compile and test from the command line.
camickra at 2007-7-8 23:30:01 > top of Java-index,Java Essentials,Java Programming...
# 9
You can also do this to see where the class exists at runtime, then eyeball it to see if the image is there. too:URL url = getClass().getResource(".");System.out.println(url);
DrLaszloJamfa at 2007-7-8 23:30:01 > top of Java-index,Java Essentials,Java Programming...
# 10

> You can also do this to see where the class exists at

> runtime, then eyeball it to see if the image is

> there. too:

I tried that. My program is laid out like this:

Game

-package1

package2

package3

The image (and the .class) is inside the package2 folder, but when I tried the code you gave me, it returned the URL of the game folder. I'll try to make a jar file (which I was going to do eventually) and see if that makes a difference.

Rob_Ha at 2007-7-8 23:30:01 > top of Java-index,Java Essentials,Java Programming...