1
public void saveImage(String path){
    BufferedImage image = (BufferedImage) createImage(500, 500);
    Graphics gImage = image.getGraphics(); //<<<<<<<<--- exception
    paint(gImage);
    image = image.getSubimage(0,0,500,500);     
    try {
        ImageIO.write(image, "png", new File(path+".png"));
    }
    catch (Exception e){}
}

Where is the problem ??

1
  • Thanks, but i need more help .. i am using a timer to display an arraylist of JPanels, when the timer is stopped the method .isDisplayable() returns false and the exception appears, otherwise the image is saved ... so what should i do ?? Commented May 5, 2011 at 7:46

2 Answers 2

2

Apparently the method createImage(int, int) is returning null. The reason is explained in the documentation:

Returns:
an off-screen drawable image, which can be used for double buffering. The return value may be null if the component is not displayable. This will always happen if GraphicsEnvironment.isHeadless() returns true.

Sign up to request clarification or add additional context in comments.

Comments

0

Your NPE is probably not coming from getGraphics but from trying to dereference a null image variable. If your component is not displayable, createImage returns null.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.