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

2017/10/1

Null Pointer exception

Alex12 Alex12

2017/10/1

#
Hey guys, so I am basically making a very simple game where two cars have to chase "food"(Ladung) and have to avoid getting hit by a meteor. If a certain score is hit by , say car1, I want to call a method from car2 where the game stops. I know this is a complicated way to do it but its for school :) Car1 code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car extends Actor
{
    int load = 0;
    int health = 10;
    int score = 1;

    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

        steuerung();
        touching();
        punktestand();
        endgame();
    }    

    public int getLoad()
    {
        return score;
    }

    public void steuerung()
    {

        if (Greenfoot.isKeyDown("w")) 
        {
            setLocation(getX(), getY()-8);
        }

        if (Greenfoot.isKeyDown("s")) 
        {
            setLocation(getX(), getY()+8);
        }

        if (Greenfoot.isKeyDown("d")) 
        {
            setLocation(getX()+4, getY());
        }

        if (Greenfoot.isKeyDown("a")) 
        {
            setLocation(getX()-4, getY());
        }
    }

    public void touching()
    {
        if(isTouching(Meteor.class))
        {
            health = health - 1;
            removeTouching(Meteor.class);
        }
        if(isTouching(Ladung.class) && load < 10)
        {
            load++;
            removeTouching(Ladung.class);

        }

    }

    public void punktestand()
    {

        if(isTouching(Droppoint.class))
        {
            score = score + load;
            load = load - load;
        }
    }

    public void endgame()
    {
        Actor actor = getOneIntersectingObject(Actor.class);
        Car2 car2 = (Car2) actor;
        int car2score = car2.getLoad();
        if(car2score > 20)
        {
            getWorld().showText("Player 2 wins!",300, 200);
            Greenfoot.stop();
        }
    }
}
Car2 code (basically the same but with var names changed)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car2 extends Actor
{
    int load2 = 0;
    int health2 = 10;
    int score2 = 1;
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

        steuerung();
        touching();
        punktestand();
        endgame();
    }    
    public int getLoad()
    {
        return score2;
    }
    public void steuerung()
    {

        if (Greenfoot.isKeyDown("up")) 
        {
            setLocation(getX(), getY()-8);
        }

        if (Greenfoot.isKeyDown("down")) 
        {
            setLocation(getX(), getY()+8);
        }

        if (Greenfoot.isKeyDown("right")) 
        {
            setLocation(getX()+4, getY());
        }

        if (Greenfoot.isKeyDown("left")) 
        {
            setLocation(getX()-4, getY());
        }
    }

    public void touching()
    {
        if(isTouching(Meteor.class))
        {
            health2 = health2 - 1;
            removeTouching(Meteor.class);
        }
        if(isTouching(Ladung.class) && load2 < 10)
        {
            load2++;
            removeTouching(Ladung.class);

        }

    }

    public void punktestand()
    {
        

        if(isTouching(Droppoint.class))
        {
            score2 = score2 + load2;
            load2 = load2 - load2;
        }

    }
    public void endgame()
    {
        Actor actor = getOneIntersectingObject(Actor.class);
        Car car = (Car) actor;
        int carscore = car.getLoad();
        if(carscore > 20)
        {
            getWorld().showText("Player 1 wins!",300, 200);
            Greenfoot.stop();
        }
    }
}
Hope you can help me, thanks in advance.
Alex12 Alex12

2017/10/1

#
The method which the game says is wrong is the endgame() in both cars.
danpost danpost

2017/10/1

#
Why are you checking for intersecting cars in the endgame metthods?
Alex12 Alex12

2017/10/1

#
Thats how our teacher taught us to access a var from another actor. Searched for other methods but didn't find anything working for me.
danpost danpost

2017/10/1

#
If each car is to continuously check the score of the other (regardless of whether they intersect or not), then use a method that does not require touching, like:
Car car = (Car) getObjectsInRange(775,Car.class).get(0);
Alex12 Alex12

2017/10/1

#
This is working really well. Thank you for your help. :)
You need to login to post a reply.