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

2019/11/4

Greenfoot Bug again? - NullpointerException

ITStudent ITStudent

2019/11/4

#
So i just want a score and thats my script. On world CrabWorldSlither the score works fine, but in crabworld it says NullpointerException. I dont know why and there are no errors in the script so... My script: if(getWorld() instanceof CrabWorldSlither){if(isTouching(Craby.class)){this.setLocation(Greenfoot.getRandomNumber(500), Greenfoot.getRandomNumber(500)); op++; }} if (getWorld() instanceof CrabWorld){if(isTouching(Craby.class)){op++; getWorld().removeObject(this); }} getWorld().showText("Score:"+op, 400,60);
danpost danpost

2019/11/4

#
You cannot call a method (showText -- on last line) on a non-existent object. The reference to a World object returned by getWorld will be null if this actor is removed from the world (previous line). Change last line to:
if (getWorld() != null)  getWorld().showText("Score:"+op, 400, 60);
You need to login to post a reply.