Solved

loading an image

Posted on 2004-03-28
12
269 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now