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

2017/9/11

Making a check if a list is empty

GustavoA GustavoA

2017/9/11

#
Hi, I am trying to create a scoreboard, but I have ran into some troubles. Basically my game involves two characters, fighting, where one dies. When a character dies, the game should reset, and a point given respectively to the surviving character. Thing is, the way I figured I could make the check for this does not really work:
    public void resetGame(){
        if(getWorld().getObjects(Hedgehog.class) != null){
            ++score1;
        }
        else{
            ++score2;
        }
        scoreboard();
        respawnCharacters();
    }
Any suggestions to how I can make this run is appreciated, thanks!
GustavoA GustavoA

2017/9/11

#
Here's the error by the way:
java.lang.NullPointerException at GameSettings.resetGame(GameSettings.java:28) at Character.gotHit(Character.java:75) at Leaf.checkCollision(Leaf.java:49) at Leaf.act(Leaf.java:28) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
EDIT: Made the if-statement do "if(getWorld().getObjects(Hedgehog.class).size() != 0)" instead, still gives same error.
danpost danpost

2017/9/11

#
The problem is the use of 'getWorld'. If you would show a bit more of the GameSettings class (either the entire thing, or even just either the 'scoreboard' or 'respawnCharacters' metthods), it should, hopefully, enlighten us as to what you need to do to fix it.
GustavoA GustavoA

2017/9/12

#
I made a rework that made it work. Removed the GameSettings class and moved the scoreboard etc. to my world subclass.
You need to login to post a reply.