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

2017/2/6

NullPointerException at the end of my game...

TheGreenFoot TheGreenFoot

2017/2/6

#
Hi guys, I am still working on my Pacman game. I wrote a code so it removes all objects and then creates a "Winner" object based on which Pacman died, but when it gets to that point, I get this error: java.lang.NullPointerException at greenfoot.World.addObject(World.java:412) at PacmansWorld.pacman2Death(PacmansWorld.java:74) at Pacman2.die(Pacman2.java:157) at Ghost.lookForPacman2(Ghost.java:61) at Ghost.act(Ghost.java:23) at Blinky.act(Blinky.java:17) 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) java.lang.NullPointerException at greenfoot.World.addObject(World.java:412) at PacmansWorld.pacman2Death(PacmansWorld.java:75) at Pacman2.die(Pacman2.java:157) at Ghost.lookForPacman2(Ghost.java:61) at Ghost.act(Ghost.java:23) at Blinky.act(Blinky.java:17) 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) java.lang.NullPointerException at greenfoot.World.addObject(World.java:412) at PacmansWorld.pacman2Death(PacmansWorld.java:75) at Pacman2.die(Pacman2.java:157) at Ghost.lookForPacman2(Ghost.java:61) at Ghost.act(Ghost.java:23) at Blinky.act(Blinky.java:17) 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) java.lang.NullPointerException at greenfoot.World.addObject(World.java:412) at PacmansWorld.pacmanDeath(PacmansWorld.java:68) at Pacman.die(Pacman.java:157) at Ghost.lookForPacman(Ghost.java:51) at Ghost.act(Ghost.java:22) at Blinky.act(Blinky.java:17) 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) java.lang.NullPointerException at greenfoot.World.addObject(World.java:412) at PacmansWorld.pacman2Death(PacmansWorld.java:75) at Pacman2.die(Pacman2.java:157) at Ghost.lookForPacman2(Ghost.java:57) at Ghost.act(Ghost.java:23) at Pinky.act(Pinky.java:17) 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) java.lang.NullPointerException at greenfoot.World.addObject(World.java:412) at PacmansWorld.pacman2Death(PacmansWorld.java:76) at Pacman2.die(Pacman2.java:157) at Ghost.lookForPacman2(Ghost.java:57) at Ghost.act(Ghost.java:23) at Pinky.act(Pinky.java:17) 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) java.lang.NullPointerException at greenfoot.World.addObject(World.java:412) at PacmansWorld.pacmanDeath(PacmansWorld.java:68) at Pacman.diediedie(Pacman.java:157) at Ghost.lookForPacman(Ghost.java:47) at Ghost.act(Ghost.java:22) at Pinky.act(Pinky.java:17) 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) java.lang.NullPointerException at greenfoot.World.addObject(World.java:412) at PacmansWorld.pacmanDeath(PacmansWorld.java:68) at Pacman.diediedie(Pacman.java:157) at Ghost.lookForPacman(Ghost.java:47) at Ghost.act(Ghost.java:22) at Blinky.act(Blinky.java:17) 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) Here is my world code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class PacmansWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PacmansWorld extends World
{
    private Pacman thePacman;
    private Pacman2 thePacman2;
    private Initializer theInitializer;
    private Winner theWinner;
    private int initialize;
    
    /**
     * Constructor for objects of class PacmansWorld.
     * 
     */
    public PacmansWorld()
    {    
        super(1200, 750, 1);
        Greenfoot.setSpeed(43);
        setBackground("white.png");
        theInitializer = new Initializer();
        addObject(theInitializer, 600, 375);
        initialize = 0;
    }
    
    public void act()
    {
        checkInitialize();
    }
    
    public void checkInitialize()
    
    {
        if (initialize == 250)
        {
            removeObject(theInitializer);
            setBackground("background.jpg");
            thePacman = new Pacman();
            thePacman2 = new Pacman2();
            addObject(thePacman, 5, 5);
            addObject(thePacman2, 5, 445);
            spawnDots();
            spawnGhosts();
        }
        initialize ++;
    }
    
    public Pacman getPacman()
    {
        return thePacman;
    }
    
    public Pacman2 getPacman2()
    {
        return thePacman2;
    }
    
    
    public void pacmanDeath()
    {
        endGame();
        Greenfoot.delay(5);
        addObject(theWinner, 600, 375);
        theWinner.p1();
        theWinner.whoWins();
    }
    
    public void pacman2Death()
    {
        endGame();
        addObject(theWinner, 600, 375);
        theWinner.p2();
        theWinner.whoWins();
    }
    
    
    public void spawnDots ()
    
    {
        for (int i = 1; i<=10; i++)
        {
            addObject(new Pill(), Greenfoot.getRandomNumber(500), Greenfoot.getRandomNumber(500));
        }
    }
    
    public void spawnGhosts ()
    
    {
        addObject(new Blinky(), 845, 250);
        addObject(new Pinky(), 845, 250);
    }
    
    public void endGame()
    {
       removeObjects(getObjects(null));
        
    }
}
Here my Ghost class code (Blinki and Pinky are subclasses of 'Ghost'):
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * Hummer (Lobster). Hummer leben auf dem Strand. Sie fressen gerne Krabben. (Na ja, 
 * zumindest in unserem Spiel...)
 * 
 * Version: 2
 * 
 * Der Hummer bewegt sich nach dem Zufallsprinzip. Wenn er auf eine Krabbe trifft,
 * frisst er sie. In dieser Version haben wir Sound hinzugefügt und das Spiel endet,
 * wenn ein Hummer die Krabbe frisst.
 */

public class Ghost extends Animal
{

    public void act()
    {
        turnAtEdge();
        randomTurn();
        move();
        lookForPacman();
        lookForPacman2();
    }

    public void turnAtEdge()
    {
        if ( atWorldEdge() ) 
        {
            turn(17);
        }
    }

    public void randomTurn()
    {
        if (Greenfoot.getRandomNumber(100) > 90) {
            turn(Greenfoot.getRandomNumber(90)-45);
        }
    }

    public void lookForPacman()
    {
        if ( canSee(Pacman.class) ) 
        {
           PacmansWorld pacmansworld = (PacmansWorld) getWorld();
           Pacman pacman = pacmansworld.getPacman();
           pacman.diediedie();
        }
    }
    
    public void lookForPacman2()
    {
        if ( canSee(Pacman2.class) ) 
        {
           PacmansWorld pacmansworld = (PacmansWorld) getWorld();
           Pacman2 pacman2 = pacmansworld.getPacman2();
           pacman2.diediedie();
        }
    }
}
Here the 'Pacman' class code:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.Random;
import java.util.List;
import java.util.ArrayList;


public class Pacman extends Animal
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage die1;
    private GreenfootImage die2;
    private GreenfootImage die3;
    private GreenfootImage die4;
    private GreenfootImage die5;
    private GreenfootImage die6;
    private GreenfootImage die7;
    private GreenfootImage die8;
    private int pillsEaten;
    private int whichImage;
    public GreenfootSound wakawaka = new GreenfootSound("wakawaka.mp3");
    public GreenfootSound gameover = new GreenfootSound("death.wav");
    private static final int EAST = 0;
    private static final int WEST = 1;
    private static final int NORTH = 2;
    private static final int SOUTH = 3;

    private int direction;
    
    /**
     * Erzeugt einen Pacman und initialisiert  beide Bilder.
     */
    public Pacman()
    {
        image1 = new GreenfootImage("Pacman.png");
        image2 = new GreenfootImage("Pacman2.png");
        die1 = new GreenfootImage("phase1.png");
        die2 = new GreenfootImage("phase2.png");
        die3 = new GreenfootImage("phase3.png");
        die4 = new GreenfootImage("phase4.png");
        die5 = new GreenfootImage("phase5.png");
        die6 = new GreenfootImage("phase6.png");
        die7 = new GreenfootImage("phase7.png");
        die8 = new GreenfootImage("phase8.png");
        pillsEaten = 0;
        setImage(image1);
        whichImage = 1;
        wakawaka.play();
        setDirection(EAST);
    }
 
    public void act()
    {
        checkKeypress();
        eatThings();
        switchImage();
    }
    
    /**
     * Wechselt das Bild des Pacman zwischen Pacman und ClosedPacman.
     */
    public void switchImage()
    {
        if (whichImage == 1)
        {
            setImage(image2);
            whichImage = 2;
        }
        else
        {
            setImage(image1);
            whichImage = 1;
        }
    }
            
    /**
     * Prüft, ob eine Steuertaste auf der Tastatur gedrückt wurde.
     * Wenn ja, reagiert die Methode entsprechend.
     */
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("A")) 
        {
            setDirection(WEST);
            move();
            move();
        }
        
        else if(Greenfoot.isKeyDown("D"))
        {
            setDirection(EAST);
            move();
            move();
        }
        else if(Greenfoot.isKeyDown("W"))
        {
            setDirection(NORTH);
            move();
            move();
        }
         else if(Greenfoot.isKeyDown("S"))
        {
            setDirection(SOUTH);
            move();
            move();
        }
    }

    public void eatThings()
    {
        if (isTouching(Pill.class))
        {
            removeTouching(Pill.class);
        }
    }
    
    /**
     * Prüft, ob wir auf eine Pille gestoßen sind.
     * Wenn ja, wird sie gefressen. Wenn nein, passiert nichts.
     */
    //**public void eatPills()
    //    if ( canSee(Pill.class) ) 
    //    {
    //        eat(Pill.class);
    //        Greenfoot.playSound("slurp.wav");                 
    //        pillsEaten ++;
    //        if (pillsEaten > 5)
    //        {
    //            Greenfoot.playSound("fanfare.wav");
    //            Greenfoot.stop();
    //        }    
    //    }
    //}
    
    public void diediedie()
    {
        wakawaka.stop();
        gameover.play();
        Greenfoot.delay(2);
        setImage(die1);
        Greenfoot.delay(2);
        setImage(die2);
        Greenfoot.delay(2);
        setImage(die3);
        Greenfoot.delay(2);
        setImage(die4);
        Greenfoot.delay(2);
        setImage(die5);
        Greenfoot.delay(2);
        setImage(die6);
        Greenfoot.delay(2);
        setImage(die7);
        Greenfoot.delay(2);
        setImage(die8);
        Greenfoot.delay(2);
        PacmansWorld pacmansworld = (PacmansWorld) getWorld();
        pacmansworld.pacmanDeath();
    }
    
    public void stopPlaying()
    {
        wakawaka.stop();
    }
    public void setDirection(int direction)
    {
        this.direction = direction;
        switch(direction) {
            case SOUTH :
                setRotation(90);
                break;
            case EAST :
                setRotation(0);
                break;
            case NORTH :
                setRotation(270);
                break;
            case WEST :
                setRotation(180);
                break;
            default :
                break;
        }
    }


}
Pacman2 (very similar to Pacman):
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.Random;
import java.util.List;
import java.util.ArrayList;


public class Pacman2 extends Animal
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage die1;
    private GreenfootImage die2;
    private GreenfootImage die3;
    private GreenfootImage die4;
    private GreenfootImage die5;
    private GreenfootImage die6;
    private GreenfootImage die7;
    private GreenfootImage die8;
    private int pillsEaten;
    private int whichImage;
    public GreenfootSound wakawaka = new GreenfootSound("wakawaka.mp3");
    public GreenfootSound gameover = new GreenfootSound("death.wav");
    private static final int EAST = 0;
    private static final int WEST = 1;
    private static final int NORTH = 2;
    private static final int SOUTH = 3;

    private int direction;
    
    /**
     * Erzeugt einen Pacman und initialisiert  beide Bilder.
     */
    public Pacman2()
    {
        image1 = new GreenfootImage("Pacman.png");
        image2 = new GreenfootImage("Pacman2.png");
        die1 = new GreenfootImage("phase1.png");
        die2 = new GreenfootImage("phase2.png");
        die3 = new GreenfootImage("phase3.png");
        die4 = new GreenfootImage("phase4.png");
        die5 = new GreenfootImage("phase5.png");
        die6 = new GreenfootImage("phase6.png");
        die7 = new GreenfootImage("phase7.png");
        die8 = new GreenfootImage("phase8.png");
        pillsEaten = 0;
        setImage(image1);
        whichImage = 1;
        setDirection(EAST);
    }
 
    public void act()
    {
        checkKeypress();
        eatThings();
        switchImage();
    }
    
    /**
     * Wechselt das Bild des Pacman zwischen Pacman und ClosedPacman.
     */
    public void switchImage()
    {
        if (whichImage == 1)
        {
            setImage(image2);
            whichImage = 2;
        }
        else
        {
            setImage(image1);
            whichImage = 1;
        }
    }
            
    /**
     * Prüft, ob eine Steuertaste auf der Tastatur gedrückt wurde.
     * Wenn ja, reagiert die Methode entsprechend.
     */
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("Left")) 
        {
            setDirection(WEST);
            move();
            move();
        }
        
        else if(Greenfoot.isKeyDown("Right"))
        {
            setDirection(EAST);
            move();
            move();
        }
        else if(Greenfoot.isKeyDown("Up"))
        {
            setDirection(NORTH);
            move();
            move();
        }
         else if(Greenfoot.isKeyDown("Down"))
        {
            setDirection(SOUTH);
            move();
            move();
        }
    }

    public void eatThings()
    {
        if (isTouching(Pill.class))
        {
            removeTouching(Pill.class);
        }
    }
    
    /**
     * Prüft, ob wir auf eine Pille gestoßen sind.
     * Wenn ja, wird sie gefressen. Wenn nein, passiert nichts.
     */
    //**public void eatPills()
    //    if ( canSee(Pill.class) ) 
    //    {
    //        eat(Pill.class);
    //        Greenfoot.playSound("slurp.wav");                 
    //        pillsEaten ++;
    //        if (pillsEaten > 5)
    //        {
    //            Greenfoot.playSound("fanfare.wav");
    //            Greenfoot.stop();
    //        }    
    //    }
    //}
    
    public void diediedie()
    {
        PacmansWorld pacmansworld = (PacmansWorld) getWorld();
        Pacman pacman = pacmansworld.getPacman();
        pacman.stopPlaying();
        gameover.play();
        Greenfoot.delay(2);
        setImage(die1);
        Greenfoot.delay(2);
        setImage(die2);
        Greenfoot.delay(2);
        setImage(die3);
        Greenfoot.delay(2);
        setImage(die4);
        Greenfoot.delay(2);
        setImage(die5);
        Greenfoot.delay(2);
        setImage(die6);
        Greenfoot.delay(2);
        setImage(die7);
        Greenfoot.delay(2);
        setImage(die8);
        Greenfoot.delay(2);
        pacmansworld.pacman2Death();
    }
    
    public void setDirection(int direction)
    {
        this.direction = direction;
        switch(direction) {
            case SOUTH :
                setRotation(90);
                break;
            case EAST :
                setRotation(0);
                break;
            case NORTH :
                setRotation(270);
                break;
            case WEST :
                setRotation(180);
                break;
            default :
                break;
        }
    }


}
The 'Winner' code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Winner here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Winner extends Actor
{
    /**
     * Act - do whatever the Winner wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private int winner;
    
    public Winner()
    {
        winner = 0;
    }
    
    public void act() 
    {
        // Add your action code here.
    }   
    
    public void p1()
    {
        winner = 1;
    }
    
    public void p2()
    {
        winner = 2;
    }
    
    public void whoWins()
    {
        if (winner == 1)
        {
            setImage("1wins.png");
        }
        else if (winner == 2)
        {
            setImage("2wins.png");
        }
    }
}
I got no compiler mistakes, just this error after removing all objects. What did I do wrong?
Super_Hippo Super_Hippo

2017/2/6

#
You never create Winner object. You need a line like:
theWinner = new Winner();
You can do that when creating the field (line 14), in the constructor or right before it is added to the world. If you take the last option, you don't even need to have an instance field for it.
TheGreenFoot TheGreenFoot

2017/2/6

#
what else do I have to do if i create it in the constructor? i added your line in the constructor and still get an error.
Super_Hippo Super_Hippo

2017/2/6

#
You got the Nullpointer Exception because you called methods on 'theWinner' even though it was null. If you added it to the constructor, it should not be null anymore (unless you set it to null again from somewhere else). Is the error still pointing to the same line (68 or 76 depending on who won)?
TheGreenFoot TheGreenFoot

2017/2/7

#
Yes, there is an error in line 77 and 78 java.lang.NullPointerException at greenfoot.World.addObject(World.java:412) at PacmansWorld.pacman2Death(PacmansWorld.java:77) at Pacman2.diediedie(Pacman2.java:157) at Ghost.lookForPacman2(Ghost.java:57) at Ghost.act(Ghost.java:23) at Blinky.act(Blinky.java:17) 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) java.lang.NullPointerException at greenfoot.World.addObject(World.java:412) at PacmansWorld.pacman2Death(PacmansWorld.java:78) at Pacman2.diediedie(Pacman2.java:157) at Ghost.lookForPacman2(Ghost.java:57) at Ghost.act(Ghost.java:23) at Blinky.act(Blinky.java:17) 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)
TheGreenFoot TheGreenFoot

2017/2/7

#
Thanks for your help, I just mistyped "winner" "winnner" in your line, thank you ^^
TheGreenFoot TheGreenFoot

2017/2/7

#
But if I die with the "Pacman" class now, 1 get this error: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:711) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:913) at Animal.canSee(Animal.java:74) at Ghost.lookForPacman2(Ghost.java:53) at Ghost.act(Ghost.java:23) at Blinky.act(Blinky.java:17) 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) It doesn't appear when dying with Pacman 2.
Super_Hippo Super_Hippo

2017/2/7

#
Do you remove the Ghost from the world if it killed a Pacman? In the code above, I don't see this happening.
TheGreenFoot TheGreenFoot

2017/2/7

#
removeObjects(getObjects(null));
but this removes all objects, doesn't it?
Super_Hippo Super_Hippo

2017/2/7

#
Oh yes, there it is. You can either add this line at the start of the lookForPacman2 method...
if (getWorld() == null) return;
... or you put both methods into one:
public void lookForPacmans()
{
    if ( canSee(Pacman.class) ) 
    {
       PacmansWorld pacmansworld = (PacmansWorld) getWorld();
       Pacman pacman = pacmansworld.getPacman();
       pacman.diediedie();
    }
    else if ( canSee(Pacman2.class) ) 
    {
       PacmansWorld pacmansworld = (PacmansWorld) getWorld();
       Pacman2 pacman2 = pacmansworld.getPacman2();
       pacman2.diediedie();
    }
}
danpost danpost

2017/2/7

#
Try moving lines 43 and 44 of the PacmansWorld class up into the constructor (around line 25 or so).
TheGreenFoot TheGreenFoot

2017/2/7

#
Thank you so much for your help, works fine now!
You need to login to post a reply.