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

2020/9/29

NullPointerException

abcd321 abcd321

2020/9/29

#
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)
Nosson1459 Nosson1459

2020/9/29

#
I'm not really sure what is going on your post is a little vague but it seems like your trying to check Actor.class when your not actually touching it.
Nosson1459 Nosson1459

2020/9/29

#
if you can post the whole class code using code tags it would be a lot easier
Super_Hippo Super_Hippo

2020/9/30

#
I never used the Timer and TimerTask classes (and I don’t see a reason why you do) but the problem might be happening because each act cycle, you create new Timer and TimerTask objects with every Body object which are executing code including the intersection check 1000 times a second.
You need to login to post a reply.