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

2014/11/12

Unable to pass the value of a collectable

connorxclose connorxclose

2014/11/12

#
Currently, I am creating a maze game and I am at the final steps. The fly moves around the maze eating flowers being careful not to be eaten by the spiders. If the fly is eaten before eating a single flower the score of 0 is passed through to the game over screen. However, when the fly eats a flower, the flower is removed and when I try to pass the value of the actor to the counter the game crashes with the error code java.lang.nullpointerexception Will post code if anyone asks to see
danpost danpost

2014/11/12

#
Please clear the terminal, reproduce the error and copy/paste the terminal error message in its entirely here.
connorxclose connorxclose

2014/11/12

#
java.lang.NullPointerException at Fly.eatFlower(Fly.java:32) at Fly.act(Fly.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:583) at greenfoot.core.Simulation.runOneLoop(Simulation.java:541) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2014/11/12

#
The first line that shows a method/class you included in your project -- this line: at Fly.eatFlower(Fly.java:32) indicates that the error occurred in the 'eatFlower' method, at line 32 of your Fly class. Please post the code in your Fly class.
connorxclose connorxclose

2014/11/12

#
Line 32 is the scoreCounter.add(20); Here is the code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Fly here. * * @author (your name) * @version (a version number or a date) */ public class Fly extends Animal { public static Counter scoreCounter; int flowersEaten; /** * Act - do whatever the Wombat wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { isKeyDown(); eatFlower(); } public void eatFlower() { Actor Flower; Flower = getOneObjectAtOffset(0, 0, Flower.class); if (Flower !=null) { World world; world = getWorld(); world.removeObject(Flower); scoreCounter.add(20); } flowersEaten = flowersEaten +1; if (flowersEaten == 5) { Existance existance = (Existance) getWorld(); Greenfoot.stop(); } } }
danpost danpost

2014/11/12

#
That means that you have not set a Counter object to the 'scoreCounter' class field.
You need to login to post a reply.