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

2013/10/25

List question

ddvink ddvink

2013/10/25

#
 public void checkForNeighbour(){
        List<Platform> pf = getWorld().getObjects(Platform.class);
        
        for (Platform p: pf){
            System.out.println("List item: " + p);
        }
    }
I have three objects of platforms in my world. But this is giving me a nullpointerexception. Can someone explain? Yours, Dennis
Gevater_Tod4711 Gevater_Tod4711

2013/10/25

#
In which line is the NullPointerException? If it's in line 2 your actor is not in the world when you execute this so getWorld() return null and you get the exception. If it's in line 4 you probably have a problem with the iterator. You should try checking whether the list is empty or null before executing the loop (in this case it can't be null but usually you should check that).
ddvink ddvink

2013/10/25

#
It's in line 2, but I dont really understand what's going wrong....
Gevater_Tod4711 Gevater_Tod4711

2013/10/25

#
You probably removed the actor from the world and executed the method after that. E.g. when you have a getWorld().removeObject(this) in the act method and after this line you call the method checkForNeighbour() you get this error.
ddvink ddvink

2013/10/25

#
thnx.... it's working
You need to login to post a reply.