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

2022/1/11

java.lang.NullPointerException while trying to put objects in different locations

TIM187 TIM187

2022/1/11

#
Hi Guysi have Problem concerning my PONG game. Id like the paddles to reset on their location off the beginning off the game. Ive done if the ball has a certain Y Coordinate it needs to reset the ball and set the counter higher. This works just fine, until I add this line of Code :
Actor Rightplayer = getOneIntersectingObject(Rightplayer.class);
            Rightplayer.setLocation(300, 200);
The Compiling is also working just fine but if the IF Statement kicks in it just gives me this error in an separate Window: java.lang.NullPointerException Here is the complete If Statement :
if(getX()<10)
            {
                getWorld() != null;
                deltaX = -7; 
                deltaY = 0;
                World myWorld = getWorld();
                this.setLocation(300,200);
               // Playerleft.setLocation(10,20);
            Background Background = (Background)myWorld;
            Scoreleft Scoreleft = Background.getScoreleft();
            Scoreleft.addScore();
            Greenfoot.delay(40);
            Actor Rightplayer = getOneIntersectingObject(Rightplayer.class);
            Rightplayer.setLocation(300, 200);
Hope y'all can help me ;-)
Super_Hippo Super_Hippo

2022/1/11

#
“getIntersectionObject” returns an intersection actor. If there is no Rightplayer actor intersecting the actor this code is in, it will return null. (Since the two paddles will probably never intersect each other, it will always be null.) The error occurs because you call “setLocation” on null.
You need to login to post a reply.