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

2016/12/30

NullPointerException

Nosson1459 Nosson1459

2016/12/30

#
import greenfoot.*; 
import java.util.Random;
public class Floor extends Actor
{
    private int floorNumber;
    private Button button;
    private Random random;
    /**
     * Makes the floor and sets a floor number.
     */
    public Floor(int floorNumber)
    {
        this.floorNumber=floorNumber;
    }
    /**
     * Adds a button set to its floor number. 
     */
    public void addedToWorld(World world)
    {
        button = new Button(floorNumber);
        world.addObject(button, getX()+78, getY());
    }
    public void act()
    {
        addPpl();
    }
    
    private void addPpl()
    {
        Building building=(Building)getWorld();
        int add=random.nextInt(50);
        if(add==25)
        {
            building.addObject(new Person(),getX()-50,getY()+8);
        }
    }
}
java.lang.NullPointerException
	at Floor.addPpl(Floor.java:31)
	at Floor.act(Floor.java:25)
	at greenfoot.core.Simulation.actActor(Simulation.java:594)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:552)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
y is this error being thrown and how do I fix it
danpost danpost

2016/12/30

#
You declared a reference field to hold a Random object; but, you never create one and assign it to that field.
greatjack greatjack

2016/12/30

#
Replace the 1st three lines of code in addppl with this
Building building=(Building)getWorld();
Random rn = new Random();       
 int add=rn.nextInt(50);
        if(add==25)
Sincerely - Yaakov (You know who I am nosson!)
Nosson1459 Nosson1459

2016/12/30

#
greatjack wrote...
Replace the 1st three lines of code in addppl with this <Code Omitted> Sincerely - Yaakov (You know who I am nosson!)
Why shouldn't I just change line 7 to
private Random random = new Random();
if it works fine (it's just adding in the " = new Random()", instead of your line of code)? (Nosson's (younger) brother :) )
Super_Hippo Super_Hippo

2016/12/30

#
I don't see a reason why you don't just use the 'Greenfoot.getRandomNumber' method instead of creating a Random object.
danpost danpost

2016/12/30

#
Super_Hippo wrote...
I don't see a reason why you don't just use the 'Greenfoot.getRandomNumber' method instead of creating a Random object.
There is really not much difference. If only getting a random number once or twice in a class, using the Greenfoot class Random object is probably the way to go. If getting a bunch of different random numbers it may be best to go ahead and create a Random object within the class. Another thing is that the Greenfoot class method only returns int values, where a Random object is capable of more than that.
Nosson1459 Nosson1459

2017/1/1

#
I actually wanted to use it so that I'll know how in case I needed to use it (somewhere else (or here)).
You need to login to post a reply.