Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

this.getClass().getResource(fileName); always returns null! <UnknownSource>

807580Feb 19 2010 — edited Feb 19 2010
I have spent hours looking through forum posts, trying various fixes, etc, but nothing works! When run from the compiler, the code is fine. When run from the JAR file, it gives me Null exception with <Unknown Source>....(I'm "resourcing" a URL which I use to instantiate my ImageIcon, which I use to instantiate a JButton)...Now, the funny thing is, is that I tried making a JAR file without my GIF images, and put it in the same directory as all my GIF files, and it did not work still. (It should have, right?)

I have another similar program which uses some of the same functions/classes, and I instantiate my ImageIcon the same exact way as in my faulty program, and yet it works fine. I am at a complete loss, here. I tried using getToolkit() to instantiate my ImageIcon with an Image instead of a URL, and I got Error in Fetching Image. I'm rather frustrated with Java right now, lol.

My code is rather bulky, so I'll just post the erred lines.
loc = "red.gif";
java.net.URL app = this.getClass().getResource(loc);
b[x][y] = new JButton(new ImageIcon(app));
I tried using the "/" at the beginning, it didn't work. I tried moving all the GIFs into a separate folder ICONS and changed loc to "ICONS/red.gif" and "/ICONS/red.gif" both of which did not work.

The above code is inside a class that extends JPanel. Please help!! It's terrible to be so close to finishing the program and to be stuck on a seemingly unsolvable problem.

If you need more information, just ask and I'll reply.

EDIT:
The URL is the one that comes out null.

Edited by: ImWired on Feb 18, 2010 9:35 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 19 2010
Added on Feb 19 2010
5 comments
2,084 views