Forum Stats

  • 3,874,145 Users
  • 2,266,674 Discussions
  • 7,911,739 Comments

Discussions

It won't let me add an image

843807
843807 Member Posts: 46,582
edited Oct 22, 2002 11:32AM in Java Applet Development
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
    843807 Member Posts: 46,582
    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
    843807 Member Posts: 46,582
    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
    843807 Member Posts: 46,582
    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
    843807 Member Posts: 46,582
    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
    843807 Member Posts: 46,582
    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.
This discussion has been closed.