View Single Post
  #1 (permalink)  
Old 07-10-2009, 01:46 AM
DCC1 DCC1 is offline
Member
 
Join Date: Jul 2009
Posts: 10
Rep Power: 0
DCC1 is on a distinguished road
Default NullPointerException even when class is instantiated as expected
Hello, I am new to Java and am practicing by writing a simple game of Pong.

I have a PongBall class that defines some simple behaviors of a pong ball, and a PongPanel class that instantiates the class into an array I have declared as follows:

private PongBall[] pongBalls = new PongBall[5];

(the goal is to have up to 5 pong balls moving independently on the screen at once)

I instantiate the class by:

Code:
		if (!ballinmotion) { //at the start of the program
			pongBalls[0] = new PongBall();
			if (pongBalls[0] instanceof PongBall) {
			i++; //so the iterator when adding balls based on score is used, it doesnt overlap the starting ball.
			debuginfo3 = "ball 0 spawned from ballinmotion check at first run through of paintComponent method";
			}
			randomstart = (int)(Math.floor(Math.random() * 4 + 1)); //random starting
			ballinmotion = true;
		}
By default, the ballinmotion boolean variable is initialized to be false.

This if statement and instantiation is done inside my "paintComponent(Graphics g)" method, and I get the exception thrown where I am trying to access this object for the first time, here:

Code:
for (int x = 0; x < pongBalls.length; x++) { //draw all the balls in their current positions
			g.setColor(Color.BLACK);
			g.fillOval(pongBalls[x].getX(), pongBalls[x].getY(), 14, 14); //generates null ptr exception...
			}
Here is the first few of dozens of lines of compiler errors:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PongPanel.paintComponent(PongPanel.java:201)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintChildren(JComponent.ja va:864)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:5 67)
at javax.swing.JComponent.paintChildren(JComponent.ja va:864)
at javax.swing.JComponent.paintToOffscreen(JComponent .java:5131)
at javax.swing.RepaintManager$PaintManager.paintDoubl eBuffered(RepaintManager.java:1475)
at javax.swing.RepaintManager$PaintManager.paint(Repa intManager.java:1406)
at javax.swing.RepaintManager.paint(RepaintManager.ja va:1220)
at javax.swing.JComponent.paint(JComponent.java:1015)

the bold line is where I tried to access the object for the first time;
Code:
for (int x = 0; x < pongBalls.length; x++) { //draw all the balls in their current positions
			g.setColor(Color.BLACK);
			g.fillOval(pongBalls[x].getX(), pongBalls[x].getY(), 14, 14); //generates null ptr exception...
			}
It might be important to note that when I try/catch for the exception so that the window opens and displays the "debuginfo3" string, the if statement behaves as if the pongBalls[0] is an 'instanceof' the class I am trying to instantiate, "PongBall". (the debuginfo3 string displays the message defined in the body of the if statement.

If there is anything I can post to make my problem clearer, I will be happy to do so. Thanks in advance for any replies/insight as to what I am doing wrong.
Reply With Quote