• Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying an image using ImageIcon

 
Kris Van Strijdonck
Greenhorn
Posts: 8
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm a beginner and currently following this Java tutorial: http://zetcode.com/tutorials/javagamestutorial/basics/

I'm at the Image part of this chapter and I wrote/copied these 2 classes:

http://pastebin.com/rB1kzxBS (Board.java)
http://pastebin.com/Drtie0v5 (Image.java)

The error:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at image.Board.<init>(Board.java:17)
at image.Image.<init>(Image.java:10)
at image.Image.main(Image.java:20)

I'm fairly certain the problem is the path in this piece of code:

I've done some research and found that I should place the image in the same folder as my .java files, which I did (http://i.imgur.com/gt3EohY.png) but the problem still persists.

I'm pretty much stuck right now so any help is appreciated.
 
Campbell Ritchie
Marshal
Posts: 52548
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to provide the exact path to your image. Please tell us which folder you have the image in now. If you put the image in that folder and create a .jar file, then the image will be in the .jar and you can send it out as one file.
I can't remember the full details, but I can remember that the getResoiurce() method returns null if the resource is not found. Commonest cause: not quite right path.
If you go through the documentation you find it returns a URL object. Suggest you try this:-When you get the path right, you can delete the printing code and the != null test.

Chances are there are people who know a lot more than me. There probably won't be many people who know less than me about getResource()

And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 52548
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you use a relative path to the image? On the diagrams you showed you had the image in the src folder (I think).
Create a new folder called resources or images or something and move the .jpeg file into that.
Then try getClass().getResource("resources\\Nature.jpeg")
Does that help?
I tried it and got this
java PathDemo anaphallis.jpg images.anaphallis.jpg images/tree.jpg
getResource(anaphallis.jpg) = null

getResource(images.anaphallis.jpg) = null

getResource(images/tree.jpg) = file:/home/campbell/java/rubbish/images/tree.jpg

There is no such file as anaphallis (a kind of white flower) but there was a tree file in the images folder. Note I am using a Linux box so you would have to use \\ where I am using /
 
Campbell Ritchie
Marshal
Posts: 52548
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . Create a new folder called resources or images or something and move the .jpeg file into that. . . .
Create that new folder at the same level as src, so resources and src are both inside the same parent folder.
 
Kris Van Strijdonck
Greenhorn
Posts: 8
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie

I created a folder called "Resources" with the "nature.jpeg" file in it and placed it at the same level as src.

http://i.imgur.com/zdw2dfR.png

I also tried using a URL instead of a file name.

http://i.imgur.com/Y4HyfYw.png

While trying both solutions the same error occured as posted in my first post.

 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and placed it at the same level as src.


The image needs to be on found on your classpath, so that would be the directory where you compile the classes to (which may or may not be the same as your src folder).

Read the section from the Swing tutorial on How to Use Icons for more information.
 
Kris Van Strijdonck
Greenhorn
Posts: 8
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
and placed it at the same level as src.


The image needs to be on found on your classpath, so that would be the directory where you compile the classes to (which may or may not be the same as your src folder).

Read the section from the Swing tutorial on How to Use Icons for more information.


As you can read in my original post I already tried placing the image in the same directory as my .java files, and that didn't work but I'll read that oracle doc and see if it provides me with any useful information.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already tried placing the image in the same directory as my .java files


I didn't suggest same directory as .java files. I suggested same directory as .class files (which may or may not be the same directory as your .java files). I don't know how you have your environment set up. The key is the classpath, not the .java source files.
 
Kris Van Strijdonck
Greenhorn
Posts: 8
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so I placed my picture in the same directory as my .class files: http://i.imgur.com/Vn2VmOh.png

I wrote down the exact path as the argument for the getResource() method. (My project is called "Afbeeldingen")



Path in English:



What am I doing wrong? The image is placed in the same directory as my .class files and the path is correct.
 
Campbell Ritchie
Marshal
Posts: 52548
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try a relative path, as I said earlier. You can see the relative and absolute paths to my tree.jpg (which I have since deleted; it was a duplicate) and which worked in what I showed you then.
 
Paul Clapham
Sheriff
Posts: 21875
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Try a relative path, as I said earlier.


Not really. The getResource() method requires a URL, not a path. That's the fundamental problem here. Using something starting with "C:\" is just wrong because URLs don't look like that. If you use a relative URL, then it has to be relative to the class which "this.getClass()" identifies. So let's suppose you put your image in the same folder as that class; then the code you want is



 
Kris Van Strijdonck
Greenhorn
Posts: 8
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Try a relative path, as I said earlier. You can see the relative and absolute paths to my tree.jpg (which I have since deleted; it was a duplicate) and which worked in what I showed you then.


I already tried this but I tried it again to see if I did something wrong earlier but it doesn't work either: http://i.imgur.com/8b6DC6Q.png

The "Resources" directory contains the "Nature.jpeg" image of course.
 
Kris Van Strijdonck
Greenhorn
Posts: 8
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Campbell Ritchie wrote:Try a relative path, as I said earlier.


Not really. The getResource() method requires a URL, not a path. That's the fundamental problem here. Using something starting with "C:\" is just wrong because URLs don't look like that. If you use a relative URL, then it has to be relative to the class which "this.getClass()" identifies. So let's suppose you put your image in the same folder as that class; then the code you want is





I have of course already tried this and it doesn't find the image either using this method: http://i.imgur.com/2pp3cNo.png
 
Paul Clapham
Sheriff
Posts: 21875
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kris Van Strijdonck wrote:I have of course already tried this and it doesn't find the image either using this method: http://i.imgur.com/2pp3cNo.png


Could I ask you why you persist in documenting your problem via images on another site? My assumption is that you're doing this within Netbeans and it's doing something to confuse you. If you have your image in the same folder as the class which is trying to use it, then the code I posted will work. So presumably you don't. Perhaps there's more than one copy of the classes and you're not running the copy you think you're running. I'm not a Netbeans user so I don't know all of the things that it does; this is why we often recommend you don't use an IDE when you're learning Java as it just gets in the way.
 
Tess Jacobs
Ranch Hand
Posts: 71
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put your image in an images folder. Make sure the images folder is in your classpath. You can then access your image as follows:

new ImageIcon(getClass().getResource("/images/Nature.jpeg"));
 
Campbell Ritchie
Marshal
Posts: 52548
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The images shown look like Eclipse.

And sorry for the mistake which Paul C identified. I did mange to find my tree file earlier with a String description of its relative path.
I also said I know little about getResource.
 
Kris Van Strijdonck
Greenhorn
Posts: 8
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, I have tried all the possible solutions here and the error is not going away. Maybe I'll understand what I'm doing wrong when I learn more Java.
 
Tess Jacobs
Ranch Hand
Posts: 71
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my NetBeans installation, the code I posted earlier is able to pick up an image saved in the \build\classes\images folder.
 
Campbell Ritchie
Marshal
Posts: 52548
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, don't say, “never mid”. You have been given several possible solutions. There will be a solution. I have got one. I struggled for ten minutes and no end of beer to find it. Had I known how to spell jpg rather than jgp it would have been a lot easier


So I put that in the image folder and navigate one folder “above” the image folder.
javac image/Board.java
...
java image.ImageFrame "Show Tree" image.tree.jpg
And part of the tree appears on screen. But it is rotated

Changes:
  • 1: Start the Event Dispatch Thread explicitly.
  • 2: Use JLabel for the picture not a JPanel. Simpler than using the paint method.
  • 3: Not that we use it here: Use the paintComponent method for painting.


  • Please copy all your code to classes run from the command line and copy the Nature picture. Try again. You can see it will work.
     
    Piet Souris
    Rancher
    Posts: 1630
    35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well,

    what always works for me is the following:


    Greetz,
    Piet
     
    Kris Van Strijdonck
    Greenhorn
    Posts: 8
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Guys thank you all for having patience with me and trying to help me but I fixed it.

    I was copying and pasting my image to the folder where my .class files were located but all I had to do was create a new package in my IDE and import my img in there to be able to use

    The solution was so simple but I spent hours trying to find out what I was doing wrong and I was getting so frustrated.

    Thanks for the advice anyway guys.

     
    Campbell Ritchie
    Marshal
    Posts: 52548
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well done sorting it out
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!