• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image constructor says it's NULL

 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
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
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

classes/apackage/Main.class
/anotherpackage/Xyz.class
/resources/myicon.gif


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



hope that helped

p
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what do you mean by absolute path ?
can you give an example ?

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


p
 
Paul Clapham
Sheriff
Posts: 21113
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic