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:
Here my Ghost class code (Blinki and Pinky are subclasses of 'Ghost'):
Here the 'Pacman' class code:
Pacman2 (very similar to Pacman):
The 'Winner' code:
I got no compiler mistakes, just this error after removing all objects. What did I do wrong?
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));
}
}
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();
}
}
}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;
}
}
}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;
}
}
}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");
}
}
}

