Why is it that I get a java.lang.NullPointerException on the line:
Graphics gImage = offScreenImage.getGraphics();
When I use the following code:
import java.awt.*;
import javax.swing.*;
public class dispImage extends JFrame
{
Image offScreenImage;
public dispImage()
{
offScreenImage = createImage(800, 600);
this.setSize(200, 200);
this.show();
}
public void paint(Graphics g)
{
super.paint(g);
Graphics gImage = offScreenImage.getGraphics();
}
public static void main(
String[] args)
{
dispImage d1 = new dispImage();
}
}
... and yet, if I move the createImage() call to inside the paint() function it works fine?
I really don't want to call the createImage() function every time the frame paints itself, as this will really slow things down. I just want to call it once at the start of the program, then use that one instance throughout the program. I know it's something simple but I can't work out what!
Thanks very much for any answers,
D Taylor