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

2018/3/24

null pointer exception

LobsterL LobsterL

2018/3/24

#
    public void act() 
    {
        removeAtWorldEdge();
        move(-5);
    }

        public void removeAtWorldEdge()
    {
        if (isAtEdge())
        {
            getWorld().removeObject(this);
            spawnTimer();
            getWorld().addObject(new Car3(), 36, 36);
        }
    }
help
danpost danpost

2018/3/24

#
The actor looses the world when you remove it (see line 11). So getWorld (line 13) returns a null value. You could move line 11 down to after line 13; however, easier is just to use setLocation on this actor than to remove it and then add another Car3 object (unless this is not in the Car3 class).
LobsterL LobsterL

2018/3/24

#
Thanks you
You need to login to post a reply.