Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

loading an image

Posted on 2004-03-28
12
Medium Priority
?
278 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 240 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 240 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 240 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 75 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 60 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 240 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

604 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