• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException when trying to display image?

 
James Simpson
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This applet is a simple about the developer page in a website i'm creating for a class project. I'm trying to display an image and a bio for each different JButton. I'm having an Issue with compiling, I keep getting a NullPointerException error on this line

danPic = new ImageIcon(getClass().getResource("pics/danSkaggs.jpg"));
e
Which i'm assuming it goes null because it can't find the image based on the directory i'm giving it. However I can't understand what I can do different, I can't see any problems with how the directory is written. Th directory is pics/filename.jpg.

Here is the full source code.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
Where are the images located? Inside your jar? On the server?
 
James Simpson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still fairly new to Programming so you'll have to forgive me, I'm not sure what either of those entail. Its not a jar file I know that.
I coded the entire thing in eclipse, created a new folder in the project folder holding the java file and put three .jpg images in the pic folder.

C:\Users\****\****\Developers\src - is where the source code is and the pics folder
C:\Users\****\****\Developers\src\pics - is where the .jpg images are
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Applets are typically served as a jar file from a server. In case you did not know, jar stands for java archive which is akin to a zipped or compressed file. Applets are embedded in a web page which is served by a server. For more detailed information, check out http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html

C:\Users\****\****\Developers\src - is where the source code is and the pics folder
C:\Users\****\****\Developers\src\pics - is where the .jpg images are

Your code (java files) is under src. However, the binaries (.class files) which are the ones actually interpreted are...where? under src? Or somewhere else? Check out the physical location of the class files.
Hint pics/danSkaggs.jpg is a relative path...relative to the class files

 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if this can shed some light on your problem: Loading Images Using getResource
 
diksha singh neel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you try to give the absolute path name when referencing the pics and images?
 
James Simpson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Developers.class file is located in the src file. Should the class files be in the same folder as the pictures?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Simpson wrote:Developers.class file is located in the src file. Should the class files be in the same folder as the pictures?

Check out the link Darryl provided. It says it all
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
diksha singh neel wrote:Can you try to give the absolute path name when referencing the pics and images?

That's a very bad idea. Ask yourself this question; what happens when you deploy this applet to a server? Will the absolute path work?
 
James Simpson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright I took a look at the link and I rewrote the code.




Its still giving me a NullPointerException except this time its at line 22.

 
Gunjan Jadon
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Simpson wrote:Alright I took a look at the link and I rewrote the code.




Its still giving me a NullPointerException except this time its at line 22.



You can also use this-


Note: C:\\Users\\ranig\\My\\spaceinvaders\\ball.png is the whole path of ball.png image.

instead of this:
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Gunjan,

You should NOT be hardcoding a directory structure to load files. You can not guarantee the same structure will exist on all computers.

The better way is to use the "getResource()" method.

Read the section from the Swing tutorial on How to Use Icons for more information.
 
Gunjan Jadon
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:@Gunjan,

You should NOT be hardcoding a directory structure to load files. You can not guarantee the same structure will exist on all computers.

The better way is to use the "getResource()" method.

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



Hello Rob Camick,
Actually I faced same problem and I solved out myself using this. But you are right. This is not the correct way. Now I have to use "getResource()".
Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!