Solved

loading an image

Posted on 2004-03-28
12
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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
runtime exception 2 67
Better way to make a string with template variables in java 3 52
ejb entity bean example issue 2 43
Setup GlassFish 4 55
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

732 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