• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

ImageIcon not loading

Hi all,

Im trying to load an icon which is in a folder called icons. This icons folder is a subfolder of where the current class resides. The icon is not getting loaded although.

ImageIcon icon = new ImageIcon("Icons\\"
                                              + iconname+".jpg");

Thanks for any help
0
ram_os
Asked:
ram_os
  • 8
  • 4
  • 3
  • +2
1 Solution
 
CEHJCommented:
Try

ImageIcon icon = new ImageIcon(getClass().getResource("Icons/"
                                              + iconname+".jpg"));
0
 
ram_osAuthor Commented:

Cheers, i tried that but it throws a null pointer exception.
0
 
CEHJCommented:
The point is, the relative path you specified is not relative to where your class is, but to where the app is being run from.

Please show your package hierarchy in relation to its root.

If your Icons directory is off a classpath root, you can do

ImageIcon icon = new ImageIcon(getClass().getResource("/Icons/"
                                              + iconname+".jpg"));
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
0
 
ram_osAuthor Commented:

Hi again, sorry about delay.

My package hierarchy is com.new.config

The following code resides in this package:
ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL   = cldr.getResource("icons/ad.gif");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);

The folder icons is a sub folder of config.

But imageURL is still being set to null.

Thanks for the help
0
 
CEHJCommented:
OK, given the hierarchy mentioned:

<classpath root>
      com
            new
                  config
      icons

Your code should be

java.net.URL imageURL   = cldr.getResource("/icons/ad.gif");
0
 
CEHJCommented:
Oh  hang on:

>>The folder icons is a sub folder of config.

In that case, i'd move the folder where *i* just showed it to be (it shouldn't really be below any of your classes)
0
 
objectsCommented:
this *should* work:

java.net.URL imageURL   = getClass().getResource("icons/ad.gif");
0
 
ram_osAuthor Commented:

None of the above worked, my classpath is not set, does this matter?
0
 
CEHJCommented:
>>does this matter?

No - not if the app's running. Make sure that image is there and valid
0
 
ram_osAuthor Commented:

Its there alright and its valid, ill keep at it anyway, thanks for the comments
0
 
CEHJCommented:
Can you post the output of

<classpath root>dir /s/b

(where classpath root is the directory above com, per my example)

?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
No harm in adding the com directory's parent to the class-path.
0
 
CEHJCommented:
>>No harm in adding the com directory's parent to the class-path.

Well of course, that will alter the picture for other programs if *one is not already set* (as is reportedly the case)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Add . at the end of it
0
 
objectsCommented:
Make sure the sace is correct, as Java is case sensitive
0
 
Mayank SAssociate Director - Product EngineeringCommented:
If you're loading it as a resource, yes
0
 
CEHJCommented:
:-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 8
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now