Solved

loading an image

Posted on 2004-03-28
12
273 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 47
maven module vs maven project 3 72
learn programming 8 66
dao vs facade design patterns 2 30
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

740 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