Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

It won't let me add an image

843807Oct 20 2002 — edited Oct 22 2002
I'm trying to make a splash screen for applet and first I just want to display the image. I have this code in my init():
	Toolkit tk = Toolkit.getDefaultToolkit();
	Image splashImage = tk.getImage("R2Splash.gif");
	g.drawImage(splashImage, 30, 40, this);
It compiles, but when I try to run it, it takes me to the g.drawImage line and throws a NullPointerException.

Does this mean it's not finding the image or what?

Thanks,
Shawn

Comments

843807
It means that g was probably not initialized.. you can quickly check this by commenting out that g.drawImage line and put an if statement to check if g is null or not.. chances are you will find that it is null.. then you'll have to figure out why it is null..
843807
Right, g (a Graphics object) is null. What is it supposed to equal? The reference books just show the paint method and the g.drawImage() but they don't set g to anything.

Thanks,
Shawn
843807
Also... since you're using an applet, try not to use Toolkit as it is usually used for applications. Also, to make 'g' not null, just use it in your paint method like so:
import java.applet.*;
import java.awt.*;
public class Example extends Applet {
	Image splashImage;
	public void init() {
		splashImage = getImage(getCodeBase(),"R2Splash.gif");
	}
	public void paint(Graphics g) {
		g.drawImage(splashImage,30,40,null);
	}
}
843807
Thanks for the help. Unfortunately, I received the same exception at the same line. I understand that Applets automatically use the Paint(), but in this case, how is it accessing the paint() before the init() and setting the value of g? I just think it's stopping before it gets to that method.

Shawn
843807
Applets automatically use the Paint(), but in this
case, how is it accessing the paint() before the
init() and setting the value of g? I just think it's
stopping before it gets to that method.

Shawn
You don't use g.drawImage() in init(), you use it in paint(). Try using the exact code I gave you, and you'll see.
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 19 2002
Added on Oct 20 2002
5 comments
174 views