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

2017/5/24

I got this error when programming, don't know whats wrong plz help

newmeli20 newmeli20

2017/5/24

#
java.lang.NullPointerException at PumpkinPieShip.hitTheEdge(PumpkinPieShip.java:92) at PumpkinPieShip.bounceAtEdge(PumpkinPieShip.java:112) at PumpkinPieShip.act(PumpkinPieShip.java:15) 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)
Theonfx Theonfx

2017/5/24

#
Put your PumpkinPieShip.java code here, maybe you have a variable not inicializated :p
danpost danpost

2017/5/24

#
Try changing line 15 of the act method in the PumpkinPieShip class to:
if (getWorld() != null) bounceAtEdge();
newmeli20 newmeli20

2017/5/26

#
/** * */ public class PumpkinPieShip extends SpaceActors { private int horizontalSpeed = 0; private int verticalSpeed = 0; World myWorld = getWorld(); public void act() { bounceAtEdge(); turnOnCommand(); accelOnCommand(); move(horizontalSpeed,verticalSpeed); } public void turnOnCommand() { if(Greenfoot.isKeyDown("down")) { setRotation(90); } if(Greenfoot.isKeyDown("up")) { setRotation(270); } if(Greenfoot.isKeyDown("right")) { setRotation(360); } if(Greenfoot.isKeyDown("left")) { setRotation(180); } if(Greenfoot.isKeyDown("down")) { move(5); } if(Greenfoot.isKeyDown("up")) { move(5); } if(Greenfoot.isKeyDown("left")) { move(5); } if(Greenfoot.isKeyDown("right")) { move(5); } } public void accelOnCommand() { if (Greenfoot.isKeyDown("space")) { int rot=getRotation(); if(rot == 0) { horizontalSpeed = horizontalSpeed +1; } else if(rot == 180) { horizontalSpeed = horizontalSpeed -1; } else if(rot == 270) { verticalSpeed = verticalSpeed -1; } else if(rot == 90) { verticalSpeed = verticalSpeed +1; } } } public void move(int changeX, int changeY) { int x = getX(); int y = getY(); int newX = x + changeX; int newY = y + changeY; setLocation(newX,newY); } public String hitTheEdge() { int x = getX(); int y = getY(); int bottomSide = myWorld.getWidth() - 1; int rightSide = myWorld.getHeight() -1; if(y == 0) { return "top"; } else if (x == 0) { return "left"; } else if(x == rightSide) { return "right"; } else if(y == bottomSide) { return "bottom"; } else { return null; } } public void bounceAtEdge() { String edge = hitTheEdge(); if(edge == "top" || edge == "bottom") { verticalSpeed = verticalSpeed * -1; } else if (edge == "left" || edge == "right") { horizontalSpeed = horizontalSpeed * -1; } } }
Super_Hippo Super_Hippo

2017/5/26

#
'myWorld' will always be null because 'getWorld()' is executed before the object is added to a world. You can either remove the line and change every 'myWorld' instance to 'getWorld()', or you can add this method:
protected void addedToWorld(World w)
{
    myWorld = w;
}
danpost danpost

2017/5/26

#
Super_Hippo wrote...
'myWorld' will always be null because 'getWorld()' is executed before the object is added to a world. You can either remove the line and change every 'myWorld' instance to 'getWorld()', or you can add this method:
protected void addedToWorld(World w)
{
    myWorld = w;
}
Or move the line:
World myWorld = getWorld();
to be the first line in the hitTheEdge method.
newmeli20 newmeli20

2017/5/31

#
Thank you that did the trick.
You need to login to post a reply.