i want to increase a counter, if the actor collects an item. in my case ammunition, that he can shot.
but it doesn't work, if i touch the item this comes up:
java.lang.NullPointerException
at Ammunition.collectAmmunition(Ammunition.java:30)
at Ammunition.act(Ammunition.java:19)
at greenfoot.core.Simulation.actActor(Simulation.java:594)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:552)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
this is my code from the item, maybe someone can help me.
import greenfoot.*;
/**
* Write a description of class Ammunition here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ammunition extends Items
{
private Counter counter;
/**
* Act - do whatever the Ammunition wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
scrollingMethods();
collectAmmunition ();
}
public void collectAmmunition ()
{
Actor Player = getOneIntersectingObject (Player.class);
if (Player != null)
{
World myWorld = getWorld();
counter.add(3);
myWorld.removeObject(this);
}
}
}
