Im currently working on a Snake-like game. I am trying to stop the game once the snakes head touches its body or the map's wall. However, when I try to implement it by using getIntersectingObjects() in the Head class, Greenfoot throws a NullPointerException and I cant start the game until I restart Greenfoot completely. It doesnt happen immediatly, only after the program ran for a couple of seconds.
The exception only occurs after I add the following lines of code in the act() method of the Head class, otherwise it works fine:
if (getIntersectingObjects(Actor.class).isEmpty() != true)
if (isTouching(Apple.class) != true)
Greenfoot.stop();
or when I try to implement it by using isTouching() the program just freezes and crashes completely without a Exception
if (isTouching(Body.class) || isTouching (Wall.class))
Greenfoot.stop();
The error:
java.lang.NullPointerException
at greenfoot.collision.ibsp.IBSPColChecker.getOneIntersectingObject(IBSPColChecker.java:848)
at greenfoot.collision.ColManager.getOneIntersectingObject(ColManager.java:187)
at greenfoot.World.getOneIntersectingObject(World.java:805)
at greenfoot.Actor.getOneIntersectingObject(Actor.java:965)
at greenfoot.Actor.isTouching(Actor.java:979)
at Head.act(Head.java:25)
at greenfoot.core.Simulation.actActor(Simulation.java:567)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
at greenfoot.core.Simulation.runContent(Simulation.java:193)
at greenfoot.core.Simulation.run(Simulation.java:183)
