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

2018/3/7

java.lang.NullPointerException

Roel Roel

2018/3/7

#
I get a NullPointerException in this peace of code: public void completeLevel4() { if (this.score==7){ try { World world = (Level4) getWorld(); //further actions world.removeObjects(world.getObjects(Announcement.class)); world.addObject(new Announcement("You won"),400,400); Greenfoot.delay(200); Greenfoot.setWorld(new Begin()); }catch (ClassCastException x) {} } } I do not know why I get this error.
danpost danpost

2018/3/7

#
What actor class is the code in? Is the actor created from the class still in the world when this method is called? If not sure, show relevant codes.
Roel Roel

2018/3/7

#
The Class is not in the World when I want to call the method. Is this a problem?
Super_Hippo Super_Hippo

2018/3/7

#
Classes are never in a world. If the object is not in the world, you can't get a reference with 'getWorld'. You could save a reference while it is in the world. If it is never in the world, what is the purpose of this class?
Roel Roel

2018/3/8

#
This is the bullet class. The bullet disappears atWorldEdge. In this class the score is set because the bullet removes the enemy and set the score.
danpost danpost

2018/3/8

#
Roel wrote...
The Class is not in the World when I want to call the method. Is this a problem?
Yes. If the actor is not in a world, 'getWorld' returns a 'null' value.
Super_Hippo Super_Hippo

2018/3/8

#
If you really want to have the score in the Bullet class, then you could place this piece of code before you remove the bullet.
You need to login to post a reply.