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

2019/4/11

Null Pointer Exception

AdiBak AdiBak

2019/4/11

#
Hello, I'm making a car game where the player's car tries to dodge oncoming traffic. When the traffic reaches the bottom of the world, and if it didn't touch the player car, points are added. However, I'm receiving a null pointer exception and I'm not sure why and how to fix this. Can someone please help? Thank you. Here's the code for the Vehicle class (the traffic): import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Randomly has trucks spawn that drive down the road, will not let two trucks spawn on top of eachother * also truck will explode if it hits a laser. * @author Hubert Jackson Black III * @version v0.10 */ public class Vehicle extends Actor { private CarWorld world; private Score scTxt; public void act(){ checkCollision(); driveDown(); endOfRoad(); } public void driveDown() // moves trucks down the screen at 3 units. { setLocation(getX(), getY() + 3); } public void checkCollision() // checks if the trucks spawn on top of eachother. will remove one. { Actor truck = getOneIntersectingObject(Vehicle.class); if(truck != null) { getWorld().removeObject(truck); } } public void endOfRoad() // removes truck once it hits end of the world. { if(getY() >= getWorld().getHeight() -1) { if (getOneIntersectingObject(PlayerCar.class) == null){ world.setScore(world.getScore() + 1); scTxt.setText("Score: " + world.getScore()); } getWorld().removeObject(this); } } protected void addedToWorld(CarWorld w){ world = (CarWorld)w; scTxt = world.getObjects(Score.class).get(0); } } Here's the exception: java.lang.NullPointerException at Vehicle.endOfRoad(Vehicle.java:39) at Vehicle.act(Vehicle.java:16) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2019/4/11

#
It may have something to do with getScore in your CarrWorld class. Provide its code. Use code tags please.
AdiBak AdiBak

2019/4/11

#
Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class CarWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class CarWorld  extends World
{
    private int counter;
    private int score;
    /**
     * Constructor for objects of class CarWorld.
     *
     */
    public CarWorld()
    {   
        // Create a new world with 20x20 cells with a cell size of 10x10 pixels.
        super(600, 600, 1);
        addObject(new PlayerCar(), 305, 550); //will need this later to make car appear.
         
        counter = 0;
         
        prepare();
    }
     
    public void act()
    {
        setPaintOrder(PlayerCar.class, Line.class);
        counter++;
        if (counter == 60){
            addObject(new Line(), getWidth()/2, 90);
            counter = 0;
        }
        if(Greenfoot.getRandomNumber(100) == 2) //this will make trucks randomley apear.
        {       
            addObject(new Vehicle(), Greenfoot.getRandomNumber(200) + 200, 0); //this keeps trucks on the road  
        }
        if(Greenfoot.getRandomNumber(10) < 2) //this makes trees randomly appear.
        {
            addObject(new House(), Greenfoot.getRandomNumber(170), 0);  // keep them on left hand side of road
            addObject(new House(), Greenfoot.getRandomNumber(170) + 440, 0); //keep them on right hand side of road
        }
    }
    public int getScore(){
        return score;
    }
    public void setScore(int val){
        score = val;
    }
     
    public void prepare(){
        Score score = new Score();
        addObject(score, 80, 50);
    }
}
Super_Hippo Super_Hippo

2019/4/11

#
Change
1
protected void addedToWorld(CarWorld w){
to
1
protected void addedToWorld(World w){
You need to login to post a reply.