Hey, I've got some code that is supposed to paint part of an image on a JPanel. Unfortunately, when I run it, I get a nullPointer Exception.
[code="java"]public void paintMap()
{
Graphics g = this.getGraphic s(); //Create a new Graphics object.
Graphics2D g2d = (Graphics2D) g; //Cast it to a Graphics2D object (Has more options)
Image spaceImages = Toolkit.getDefa ultToolkit().ge tImage ( "stars.png" );
System.out.prin tln(spaceImages .getWidth(null) ); //Returns -1. Is that a problem?
g2d.drawImage(s paceImages, 0, 0, 50, 50, 0, 0, 50, 50, null);
//more code[/code]
The error occurs on the last line of code (Line 7), but I wonder if I'm not creating the image correctly, as when I try to get the width of the image it returns -1 (on line 6). Any thoughts?
[code="java"]public void paintMap()
{
Graphics g = this.getGraphic s(); //Create a new Graphics object.
Graphics2D g2d = (Graphics2D) g; //Cast it to a Graphics2D object (Has more options)
Image spaceImages = Toolkit.getDefa ultToolkit().ge tImage ( "stars.png" );
System.out.prin tln(spaceImages .getWidth(null) ); //Returns -1. Is that a problem?
g2d.drawImage(s paceImages, 0, 0, 50, 50, 0, 0, 50, 50, null);
//more code[/code]
The error occurs on the last line of code (Line 7), but I wonder if I'm not creating the image correctly, as when I try to get the width of the image it returns -1 (on line 6). Any thoughts?
Comment