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

2017/6/6

Counter NullPointerException

zomoruk zomoruk

2017/6/6

#
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:
public void callcounter()
    {
        
        ((PlagueWorld)getWorld()).theCounter.counttote();

    }
counttote() looks like that, nothing special:
public void counttote()
    {

        System.out.println(dead + " Counter funktioniert");
        dead = dead +1;
    }
The point where callcounter() gets called is part of a bigger method, I'll only post the important part of it:
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]                                
                    }
This is how the world class (PlagueWorld) looks like. It's large so I'll make the important parts bold.
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++;
       
    }

}



}
I'd appreciate any help!
davmac davmac

2017/6/6

#
Hi. The exception is happening in the callcounter method - your code is never reaching the counttote method, so that method isn't the problem:
zomoruk wrote...
java.lang.NullPointerException at Mensch.callcounter(Mensch.java:272) ...
You didn't specify, but I assume that this must be the line that the exception occurs on (i.e. line 272 of the Mensch class):
        ((PlagueWorld)getWorld()).theCounter.counttote();
To be getting a NullPointerException there, then either getWorld() is returning null, or 'theCounter' variable is null. Looking at your world code, it seems you are never initialising 'theCounter'. Instead, you are creating a local variable with the same name, on line 45:
Counter theCounter = new Counter();
To initialise the existing instance variable, you need to remove the type ("Counter") from the beginning of the above statement:
theCounter = new Counter();
In Java, "<type> <name> = <value>" always defines a new variable. If you want to assign a value to an existing variable, you must not include the "<type>" part.
zomoruk zomoruk

2017/6/8

#
davmac wrote...
Hi. The exception is happening in the callcounter method - your code is never reaching the counttote method, so that method isn't the problem:
zomoruk wrote...
java.lang.NullPointerException at Mensch.callcounter(Mensch.java:272) ...
You didn't specify, but I assume that this must be the line that the exception occurs on (i.e. line 272 of the Mensch class):
        ((PlagueWorld)getWorld()).theCounter.counttote();
To be getting a NullPointerException there, then either getWorld() is returning null, or 'theCounter' variable is null. Looking at your world code, it seems you are never initialising 'theCounter'. Instead, you are creating a local variable with the same name, on line 45:
Counter theCounter = new Counter();
To initialise the existing instance variable, you need to remove the type ("Counter") from the beginning of the above statement:
theCounter = new Counter();
In Java, "<type> <name> = <value>" always defines a new variable. If you want to assign a value to an existing variable, you must not include the "<type>" part.
You're my man! Thank you so much!
You need to login to post a reply.