This site requires JavaScript, please enable it in your browser!
Greenfoot back
Lightsaber71
Lightsaber71 wrote ...

2017/10/5

giving me an error but i have no clue why

Lightsaber71 Lightsaber71

2017/10/5

#
public void turnIfPaddle() { int ballY = getY(); Actor leftPaddle = getOneIntersectingObject(LeftPaddle.class); int leftPaddleY = leftPaddle.getY(); if(leftPaddleY - ballY < 6) { xMovement = -xMovement; Greenfoot.playSound("boop"); } }
Lightsaber71 Lightsaber71

2017/10/5

#
java.lang.NullPointerException at Ball.turnIfPaddle(Ball.java:68) at Ball.act(Ball.java:26) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
Super_Hippo Super_Hippo

2017/10/5

#
If there is no intersecting LeftPaddle object, leftPaddle is null and you can't get the y coordinate of y. Make sure leftPaddle is not null before executing any code after that.
Lightsaber71 Lightsaber71

2017/10/5

#
Do you mean there needs to be an object intersecting LeftPaddle? Sorry im very new to this
danpost danpost

2017/10/5

#
Lightsaber71 wrote...
Do you mean there needs to be an object intersecting LeftPaddle? Sorry im very new to this
No. HIppo means that you cannot get a coodinate of a non-referenced paddle. You are only changing the direction of the ball when it hits a paddle; but, this is something you need to check continuously. At those times when a paddle is not intersecting the ball, no paddle will get referenced by line 4 above. You must check to see if 'leftPaddle' points to a paddle or points to 'null' (no paddle, in this case):
if (leftPaddle == null) return;
You can place this line immediately after line 4 to abort the execution of the method if no paddle is found intersecting the ball.
Lightsaber71 Lightsaber71

2017/10/10

#
Thanks! I used this and it worked if(paddleLeft !=null) { xMovement = -xMovement; Greenfoot.playSound("boop"); }
Lightsaber71 Lightsaber71

2017/10/10

#
I have a new question but the same program, in the following code it is nearly identical but it only bounces off of the right paddle, not the left. public void changeDirectionInside() { if(xMovement < 0) { xMovement = xMovement + 2; xMovement = -xMovement; } if(xMovement > 0) { xMovement = xMovement - 2; xMovement = -xMovement; } Greenfoot.playSound("boop"); } public void turnIfPaddle() { int ballY = getY(); Actor paddleLeft = (Actor)getOneIntersectingObject(LeftPaddle.class); Actor paddleRight = (Actor)getOneIntersectingObject(RightPaddle.class); if(paddleLeft !=null) { leftPaddleY = paddleLeft.getY(); if(leftPaddleY - ballY < 25 && leftPaddleY - ballY > 0) { changeDirectionInside(); } if(leftPaddleY - ballY > 25 && leftPaddleY - ballY < 50) { changeDirectionOutside(); } if(leftPaddleY - ballY > -25 && leftPaddleY - ballY < 0) { changeDirectionInside(); } if(leftPaddleY - ballY < -25 && leftPaddleY - ballY > -50) { changeDirectionOutside(); } } if(paddleRight !=null) { rightPaddleY = paddleRight.getY(); if(rightPaddleY - ballY < 25 && rightPaddleY - ballY > 0) { changeDirectionInside(); } if(rightPaddleY - ballY > 25 && rightPaddleY - ballY < 50) { changeDirectionOutside(); } if(rightPaddleY - ballY > -25 && rightPaddleY - ballY < 0) { changeDirectionInside(); } if(rightPaddleY - ballY < -25 && rightPaddleY - ballY > -50) { changeDirectionOutside(); } } }
danpost danpost

2017/10/10

#
Looks like you are using the same relational operator ('>' and '<') for both left and right paddles, however, the relationship in location of the ball is different for each paddle.
You need to login to post a reply.