Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

NullPointerException when trying to display image?

 
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.

 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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
 
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
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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.

 
Ranch Hand
Posts: 45
  • 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:
 
Rancher
Posts: 3205
29
  • 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: 45
  • 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
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic