jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Image constructor says it's NULL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Image constructor says it Watch "Image constructor says it New topic

Image constructor says it's NULL

fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
I'm using JDIC.jar file to minimize my JFrame to the tray.
I create an image
ImageIcon image = new ImageIcon(System.class.getResource ("Gif_File_Path"));
I'm sure the path is correct but a NullPointerException is thrown
saying that the image is null.
Because of this I cannt create TrayIcon.
Please help!
Thank you!
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
the icon is not on the classpath or the path is not correct...

did you put "Gif_File_Path" just as a replacement for the example ?
if no:
this will look for a resource named "Gif_File_Path" which probably does not exist on yor machine... did you meant to use a reference to the String object Gif_File_Path which contains a valid value ?

e.g. String Gif_File_Path = "/blabal/myicon.gif"

btw. if you use "_" then you should write the reference name like:
GIF_FILE_PATH (and make it static/final).

resources are things that are on the classpath and so can be loaded without absolute filesystem paths.

you can change the classpath trough system variables or by specifying when starting up the program (-cp or -classpath option).
also you could put your images in the same location where your application classes are:


then you should be able to load the icon with getResource("/resource/myicon.gif") or so...

hope that helped

fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
By GIF_FILE_PATH I mean the absolute path
I'm using IntelliJ IDEA 4.5.
I put the gif file into the directory where my classes reside
and added the classes directory to the list of my libraries where
I add a jar file when I need one i.e. to my classpath
But again the same error.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
so what do you mean by absolute path ?
can you give an example ?

absolute like c:\\mydir\myfile.gif ?

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

What it says in the API documentation for getResource():
This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/".
Now, since it's the java.lang.System class that is making this call, that means that it will be looking for something named "/java/lang/GIF_FILE_PATH" with respect to your classpath. To avoid this business of making the resource relative to the class that you randomly chose to look for the resource, put a "/" on the front of the resource name as in Pascal's examples.
I agree. Here's the link: http://aspose.com/file-tools
subject: Image constructor says it's NULL
It's not a secret anymore!