Hi,
I'm new to programming and I am wanting to create a simple game. It was working fine until I added in a second counter now after the first frame gives me this error in red;
If you require any more code please feel free to ask.
Thanks in advance.
java.lang.NullPointerException
at Tardis.tryToEatB(Tardis.java:81)
at Tardis.act(Tardis.java:30
I have been trying to find a solution but have been unable to find one could anyone help please?
This code is from the main character that the user controls and is where it seems to be where the error is occurring
import greenfoot.*; import javax.swing.JOptionPane; /** * This is a turtle in a first, simple video game. It can be controlled * with the cursor keys and likes to eat ArtronEnergy. */ public class Tardis extends Characters { private Counter counter; // assigning a FIELD public Tardis(Counter pointCounter ) // CONSTRUCTOR GETS COUNTER REFERECE { counter = pointCounter; } private CounterB counterB; // assigning a FIELD public Tardis(CounterB pointCounterB ) // CONSTRUCTOR GETS COUNTER REFERECE { counterB = pointCounterB; } public void act() { move(4); checkKeys(); tryToEat(); tryToEatB(); } /** * Check whether the control keys are being pressed, and turn * if they are. */ public void checkKeys() { if ( Greenfoot.isKeyDown("left") ) { turn(-5); } if ( Greenfoot.isKeyDown("right") ) { turn(5); } } /** * Check whether we can see Sea ArtronEnergy. If we can, eat it. * WE NEED A REFERENCE TO THE COUNTER VIA WORLD OBJECT */ public void tryToEat() { if (canSee(ArtronEnergy.class) ) { eat(ArtronEnergy.class); counter.add(100); } if (counter.getValue() >= 100) { Greenfoot.setWorld(new MarsWorld()); } } public void tryToEatB() { if (canSee(ArtronEnergy.class) ) { eat(ArtronEnergy.class); counterB.add(100); } if (counterB.getValue() >= 100) { JOptionPane.showMessageDialog(null,"Victory is ours!", "You Won!",JOptionPane.INFORMATION_MESSAGE); Greenfoot.stop(); } } }