Solved

loading an image

Posted on 2004-03-28
12
272 Views
Last Modified: 2010-03-31
ive been having this problem for 2 days already.. ive been surfing the net and reading books but with no prevail i still dont know or any idea what to do with this problem.. what am i doing wrong? ive been having a NullPointerException pointing to here : setup.setImage(Toolkit.getDefaultToolkit().getImage("mpix/rmstudio.gif") ); setup is an instance of my JFrame with an attribute of Image so i could draw the image on my jframe.. this line -> setup.setImage(Toolkit.getDefaultToolkit().getImage("mpix/rmstudio.gif") ); is located on another class called Tools.. then calling setup.repaint()..i wanted to insert the image on the jframe by clicking on a jbutton ( which has the setup.setImage(Toolkit.getDefaultToolkit().getImage("mpix/rmstudio.gif") ); .. is the problem really pointing here?

Any help would be appreciated.
0
Comment
Question by:babytoy
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 80 total points
ID: 10698319
The problem may be due to Java's loading images asynchronously, which can sometimes result in their being null at the point of use. You can get around this gotcha bey using an ImageIcon

JLabel myImageLabel = new JLabel(new ImageIcon(getClass().getResource("/mpix/rmstudio.gif")));
0
 

Author Comment

by:babytoy
ID: 10698385
but by using ImageIcon, would i able to put a running image on top of it? because it is like a map (of something like this Image[14][10]) where another image (more specifically a person running on the map) runs on top of it. is that possible?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 80 total points
ID: 10698414
In that case, you're probably better off pursuing what you're already doing. Use a MediaTracker. See

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_10182719.html
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:babytoy
ID: 10698415
by using this i still get a NullPointerException but it is now pointing to the line where i added the myImageLabel to the contentpane of JFrame.
0
 

Author Comment

by:babytoy
ID: 10698416
oh i see. ill look on to that first. thanks
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 80 total points
ID: 10698434
Make sure the url is valid too if you're using the url approach i showed (better really as the resource is relocatable anywhere on the classpath):

URL imageUrl = getClass().getResource("/mpix/rmstudio.gif");

Make sure mpix is on the classpath. Test imageUrl for null
0
 
LVL 30

Assisted Solution

by:GrandSchtroumpf
GrandSchtroumpf earned 25 total points
ID: 10698439
you could also use a java.awt.MediaTracker and wait for the images to be loaded before you use them.
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 20 total points
ID: 10701736
If you are reading from a Jar then

Say your image is in package aaa;

aaa package contains your images and few classes.  (for example say it is A.class)

Then you can use, A.class.getResource("rumstudio.gif);

Use a class.getResource relative path from the class where you get the resource.


Regards,
Muruga
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 80 total points
ID: 10703262
Please read the comments already posted before posting to the thread
0
 

Author Comment

by:babytoy
ID: 10703567
A friend told me that i was passing an instance that doesn't exist.  That's why its prompting a NullPointerException.  what i need now is to load the images faster.  Fortunately there's a suggestion above.  =) Lastly, Thank you all for your kindness.  =) i really appreciate it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10703604
8-) Incidentally, the image doesn't need to be in a jar to load as i mentioned
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10706103
:°)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web application structure 18 100
Java exception bubble up 2 21
jboss 7.1 start up error 1 33
passing enum to a method 3 18
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question