• Post Reply Bookmark Topic Watch Topic
  • New Topic

class.getResource() is returning null  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you need to provide complete path.
 
Sheriff
Posts: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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: 934
4
  • Mark post as helpful
  • send pies
  • 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?
 
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where in your project is the images directory?
 
Knute Snortum
Sheriff
Posts: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Knuth.
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you and I think I'm ready to let this subject rest in peace.
 
Dave Tolls
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!