I have a plague simulation game and I want to count the dead people and output them on the terminal. I created a Counter class that is supposed to count the objects. It has a method counttote() that gets called from the 'Mensch' class whenever someone dies. I always get the following error whenever someone dies:
java.lang.NullPointerException
at Mensch.callcounter(Mensch.java:272)
at Mensch.MenschRadar(Mensch.java:245)
at Mensch.act(Mensch.java:20)
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)
This is the callcounter() method that gets called in the Mensch class:
counttote() looks like that, nothing special:
The point where callcounter() gets called is part of a bigger method, I'll only post the important part of it:
This is how the world class (PlagueWorld) looks like. It's large so I'll make the important parts bold.
I'd appreciate any help!
public void callcounter()
{
((PlagueWorld)getWorld()).theCounter.counttote();
}public void counttote()
{
System.out.println(dead + " Counter funktioniert");
dead = dead +1;
}if (Greenfoot.getRandomNumber(100)<sterbinfp)
{
Mensch msch = getObjectsInRange(radius, Mensch.class).get(0); //sterben während im Radius noch infizeirt wird
GreenfootImage schwarz;
schwarz = new GreenfootImage("gestorben.png");
msch.setImage(schwarz);
msch.setVarZustand(2);
[b] callcounter();
[/b]
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class PlagueWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class PlagueWorld extends World
{
private int i = 0;
private int Toronto = 0;
private int Chicago = 0;
private int Detroit = 0;
private int NewYork = 0;
private int Montreal = 0;
Counter theCounter;
public PlagueWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1759, 966, 1);
System.out.print('\f');
addObject(new P1(),812, 218);
addObject(new P2(),760, 336);
addObject(new P3(),793, 416);
addObject(new P4(),921, 582);
addObject(new P5(),1190, 426);
addObject(new P6(),1648, 884);
addObject(new P7(),347, 637);
addObject(new P8(),339, 493);
addObject(new P9(),352, 301);
addObject(new P10(),432, 224);
addObject(new P11(),397, 15);
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
Mensch mensch = new Mensch(x,y);
Virus virus = new Virus(x,y);
Krankenhaus krankenhaus = new Krankenhaus(x,y);
Immuner immuner = new Immuner(x,y);
[b] Counter theCounter = new Counter();
addObject(theCounter,0,0); [/b]
//added Menschen für Städte:
while (Chicago <14)
{addObject(new Mensch(1,1), Greenfoot.getRandomNumber(60)+245, Greenfoot.getRandomNumber(65)+617);//Chicago
Chicago++;
}
while (Toronto < 13)
{
addObject(new Mensch(1,1), Greenfoot.getRandomNumber(60)+1005, Greenfoot.getRandomNumber(23)+405);//Toronto
Toronto++;
}
while (Detroit < 4)
{
addObject(new Mensch(1,1), Greenfoot.getRandomNumber(60)+673, Greenfoot.getRandomNumber(51)+559);//Detroit
Detroit++;
}
while (NewYork < 42)
{
addObject(new Mensch(1,1), Greenfoot.getRandomNumber(100)+1485, Greenfoot.getRandomNumber(55)+740);//New York
NewYork++;
}
while (Montreal <9)
{
addObject(new Mensch(1,1), Greenfoot.getRandomNumber(81)+1525, Greenfoot.getRandomNumber(50)+152);//Montreal
Montreal++;
}
//Virus adden
int ivirus = 0;
while (ivirus<1)
{
x = Greenfoot.getRandomNumber(getWidth());
y = Greenfoot.getRandomNumber(getHeight());
addObject(new Virus(x,y), x,y);
ivirus++;
}
//Krankenhaus adden
int ikh = 0;
while (ikh<1)
{
x = Greenfoot.getRandomNumber(1280);
y = Greenfoot.getRandomNumber(720);
addObject(new Krankenhaus(x,y),x,y);
ikh++;
}
//Menschen adden
int imsch = 0;
while (imsch<250)
{
x = Greenfoot.getRandomNumber(getWidth());
y = Greenfoot.getRandomNumber(getHeight());
addObject(new Mensch(x,y), x,y);
imsch++;
}
}
}
