This site requires JavaScript, please enable it in your browser!
Greenfoot back
i5-2500k
i5-2500k wrote ...

2014/5/23

Calling "Counter" from different objects

i5-2500k i5-2500k

2014/5/23

#
Hi, In my game I want a counter to increase everytime an alien is getting killed. In order to do that I imported the Counter subclass and wrote the following in my world subclass to spawn it:
public class space extends World
{

    /**
     * Constructor for objects of class space.
     * 
     */
    private Counter theCounter;
    public space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        
        theCounter = new Counter();
        addObject(theCounter, 60, 360);
    }
    public Counter getCounter() {
        return theCounter;
    }

}
In the Alien-Subclass (checkHealth is also in the Act-Method)
    public void changeCounter(int score) {
        space spaceWorld = (space) getWorld();
        Counter counter = spaceWorld.getCounter();
        counter.add(score);
    }  

    public void checkHealth() {
        Actor alien = getOneIntersectingObject(alien.class);
        if (health <= 0) {
            getWorld().removeObject(alien);
            getWorld().removeObject(this);
            changeCounter(1);
        }     
    }
Sadly I'm getting the following error while hitting the Alien and setting its health to 0 and below:
java.lang.NullPointerException
	at alien.changeCounter(alien.java:69)
	at alien.checkHealth(alien.java:80)
	at alien.act(alien.java:36)
	at greenfoot.core.Simulation.actActor(Simulation.java:568)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
I'm quite new to Greenfoot and programming at all, so it'd be nice if someone knew how to handle to error. Thanks.
danpost danpost

2014/5/23

#
In your 'checkHealth' method at line 9 in the post above (line 69 of your 'alien' class), the 'if' condition is wrong. You probably wanted
if (alien != null)
But this is not the worst. The 'changeCounter(1);' line needs to be switch with the line before it to prevent the NullPointerException. What class is the 'checkHealth' method in? or, what is killing the 'alien' objects?
i5-2500k i5-2500k

2014/5/23

#
danpost wrote...
In your 'checkHealth' method at line 9 in the post above (line 69 of your 'alien' class), the 'if' condition is wrong. You probably wanted
if (alien != null)
But this is not the worst. The 'changeCounter(1);' line needs to be switch with the line before it to prevent the NullPointerException. What class is the 'checkHealth' method in? or, what is killing the 'alien' objects?
I'm not quite sure why, but switching the lines actually fixed my problem. Still, I'd appreciate if you could elaborate my mistake. The if-condition works acutally. Although I'm not quite sure if my solution is very fancy. The game-idea is pretty simple: Spaceship shooting aliens. I have a class "bullet" which decreases the aliens health as soon as the bullet hits it (using if alien != null). The given method "checkHealth" is in the Alien-class, killing the Alien the time its health hits zero.
danpost danpost

2014/5/23

#
What is the method the Bullet uses when hitting an alien? If it calls a method in the Alien-class, show it also.
You need to login to post a reply.