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

2013/5/18

java.lang.NullPointerException

Miikku Miikku

2013/5/18

#
My ScoreCounter doesen't work here is my code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Bullet extends Actor { private Counter counter; public Bullet(Counter pointCounter) { counter = pointCounter; } public void act() { kill(); move(10); remove(); } public Bullet() { setRotation(Soldier.currentRot); } public void remove() { if(getX() >= getWorld().getWidth() -1) getWorld().removeObject(this); else if(getX() <= 1) getWorld().removeObject(this); else if(getX() >= getWorld().getHeight() -1) getWorld().removeObject(this); else if(getX() <= 1) getWorld().removeObject(this); } public void kill() { Actor zombie; zombie = getOneObjectAtOffset(0, 0, Zombie.class); if (zombie != null) { counter.add(1); World world; world = getWorld(); world.removeObject(zombie); } } } Can someone help?
danpost danpost

2013/5/18

#
Stop the scenario after a zombie is hit by a bullet and right-click on the Counter object and inspect its targetValue field. If it is zero, then the counter you clicked on is not the one being incremented in the 'kill' method.
Miikku Miikku

2013/5/18

#
TargetValue is zero so what I do?
danpost danpost

2013/5/18

#
Make sure that the counter object that is in the world is the one that is passed to the bullet objects.
Miikku Miikku

2013/5/18

#
Yes it is.
danpost danpost

2013/5/18

#
Go through each class and 'Find' the string 'new Counter'. You should only find it once for this counter object.
Miikku Miikku

2013/5/18

#
I founded new Counter in my world.
danpost danpost

2013/5/18

#
Only once in the world and no where else?
Miikku Miikku

2013/5/18

#
No only in world
danpost danpost

2013/5/18

#
Is the score counter the only counter object that you are using the Counter class for?
Miikku Miikku

2013/5/18

#
Yeas.
danpost danpost

2013/5/18

#
Comment (or remove) the following lines in the Bullet class:
private Counter counter;

public Bullet(Counter pointCounter)
{
   counter = pointCounter; 
}
Then perform this change:
// change the line
counter.add(1);
// to this
((Counter)getWorld().getObjects(Counter.class).get(0)).add(1);
Miikku Miikku

2013/5/18

#
Thank you it works! You are BEST!
You need to login to post a reply.