This site requires JavaScript, please enable it in your browser!
Greenfoot back
Back to discussions
Emil
Emil wrote ...

2017/3/20

How to test if the Object is still there

Emil Emil

2017/3/20

#
Hi, i've got the following problem. I want to execute getWorld().getObjects(Bar.class); But if the Object that executes this command gets removed, I get a nullPointerException Error. To avoid this, I need to write an "if" Loop to test if the Object is still there. How do I do this? :o
Super_Hippo Super_Hippo

2017/3/20

#
You can check if the object is still in the world by checking if getWorld does not return null:
if (getWorld() != null)
{
    //do something
}
Or exit the method when it is not in the world:
if (getWorld() == null) return;

//rest of method
Maybe you can also just change the order of the method calls, so after the object was removed, no method is executed anymore which requires the actor to be in the world.
Emil Emil

2017/3/20

#
Thanks man, that works perfectly! :)
You need to login to post a reply.