• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class.getResource() is returning null

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I downloaded the Java demo program LayeredPaneDemo.

It creates an ImageIcon as follows:

where createImageIcon() is

When I run the program, imgURL in createImageIcon() is null and the image isn't loaded.

if(I create the icon as follows:

The image is loaded just fine; so I have the right path to the image file.

Any ideas about why the getResoure() call at line 4 of createImageIcon() isn't
setting imgURL properly?

I've run the program in both Eclipse and NetBeans and had the same problem in both IDEs.
I've tried the following path and still no image:
"../images/dukeRedWave.gif"
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose you need to provide complete path.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new ImageIcon takes a filename and follows the path to that file. getResources uses the ClassLoader to find the resource.
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:new ImageIcon takes a filename and follows the path to that file. getResources uses the ClassLoader to find the resource.


So that's means getResource doesn't get path of the file?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where in your project is the images directory?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:

Knute Snortum wrote:new ImageIcon takes a filename and follows the path to that file. getResources uses the ClassLoader to find the resource.


So that's means getResource doesn't get path of the file?


That's correct.
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Knuth.
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for responding. I'm sorry I didn't check back sooner.

Tushar, I tried the complete path; still doesn't work.

Dave, this is an eclipse project and the directory is as follows:

LayeredPaneDemo
....src
........mypackage
............LayeredPaneDemo.java - the entire program
....images
........dukeWaveRed.gif

Does that help?

p.s.
How can I indent to show the tree structure without using periods?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getResource() uses the ClassLoader, not a file path. Why do you want to use getResource() on an image, BTW?

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

p.s.
How can I indent to show the tree structure without using periods?



Put code tags around your text and you can indent.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two things you need to know: first, the getResource() method looks in the class path for the URL you give it. Second, if you use a relative URL (as you did) then it's relative to the fully-qualified name of the class it's used in.

So if the URL you use is "images/dukeWaveRed.gif" and it's used in a class called "biz.monkey.Demo" then the getResource() is going to look for "/biz/monkey/Demo/images/dukeWaveRed.gif" in each of your classpath roots until it finds such a gif, or until it can't find it.

Now if you don't want that relative URL handling then make it absolute, like this: "/images/dukeWaveRed.gif" and then getResource() will look for "/images/dukeWaveRed.gif" in each of your classpath roots until it finds that, or doesn't.
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

I put the fully qualified path in: didn't work.

Looking at the directory tree I put in my (last?) post where should I put the images folder to use "images/dukeWaveRed.gif?

I have put it everywhere in the tree and nothing works.


Does it care whether I use / or \?
I've tried it with / and ../ in front of images - doesn't help.

It actually doesn't really matter because I can always make my icons using the ImageIcon constructor, it's just frustrating because I should be able to use getResource().

 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you're using Eclipse. So not only do you have to learn about getResource() and how it uses the classpath, you also have to learn how Eclipse constructs its classpath for your application. Just another case of Eclipse getting in the way of you learning Java.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a quick program that prints out your classpath.



(Credit: mkyong.com)

If you want to change your classpath in Eclipse, right-click on the project in your package/project explorer, select Build Path, select Configure Build Path. This brings up the Java Build Path window.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually from what I see in that Eclipse tree, one of those guesses should be correct -- after you build the project. I always have my projects set to build automatically whenever I change something, but a lot of people don't.

And you should look in the place where Eclipse puts your class files to see where it copies the non-Java things out of your src folder to see what it's actually doing.
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul.

Building the project was not the issue.

You sparked me to realize I needed the image file with the .class file, not the .java file.

eclipse always creates bin and src folders and this is the actual directory structure eclipse created
when it created the project:

eclipse doesn't show the bin folder so I forgot that that's where the .class files are.

Once I put the images folder where I show it in the above directory, "/Images/dukeWaveRed.gif"
worked in getResources().

If I put the images folder under bin/mypackage. I'm sure "Images/dukeWaveRed.gif" (no /)
would work but I prefer to separate the images folder; that's why I had it where I originally
had it.

And now that I know how to use getResouces() when I want, is there any advantage to using it
over using the ImageIcon constructor, which I have done fairly often.
If there isn't, I think I'll continue to use the ImageIcon constructor.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would use getResources() if you wanted the application to look in the classpath for an image. So if you were distributing an application and it included some images in the distribution, then that would be the way to go -- put the images into the jar along with the Java classes. On the other hand if your application wanted to solicit images from the user and store them elsewhere, you wouldn't use getResources().
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you and I think I'm ready to let this subject rest in peace.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a way of getting Eclipse to copy other files to the bin directory as part of the build.
Sticking the images directly in the bin directory might be fragile. I don't think the bin directory is guaranteed not to be deleted as part of a build process.
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This solution works for me:
https://stackoverflow.com/questions/4301329/java-class-getresource-returns-null/50387930#50387930
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain how that solution from the SO thread would help in this instance, particularly since this is an old discussion.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the resource name begins with '/', then the path is absolute - taken from the root of the class path. Otherwise, the resource is relative to the package folder of the Class you are invoking the method upon.
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic