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

2016/4/15

NullPointerException when making a list.

jamieMatthews jamieMatthews

2016/4/15

#
My world starts by spawning a spaceship. I try to run this code after the spaceship has been spawned, yet a nullpointerexception occurs. Could you help me fix this?
public void turnTowardsSpaceship() {
            List<Spaceship> spaceships = getWorld().getObjects(Spaceship.class);
            for (Spaceship spaceship : spaceships) {
                ssX = spaceship.getX();
                ssY = spaceship.getY();
                turnTowards(ssX,ssY);
            }
    }
jamieMatthews jamieMatthews

2016/4/15

#
Okay, nevermind. The issue was I was calling this method during the preparing phase. The spaceship was already made, but I found a workaround.
You need to login to post a reply.