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

2018/6/21

Why am I getting an error?

1
2
3
Lukaas Lukaas

2018/6/21

#
I tried to add a snowman that can stand and shoot water as well but when the water of the snowman hits the plant it gives an error.
import greenfoot.*;
import java.awt.Color;

/**
 * A gardener walks around and sprays water for his plants. It's a game, so it 
 * keep tracks of the amount of points. The more points, the better of course.
 * 
 */
public class Gardener extends Actor
{
    // Field for keeping track of the points
    int points = 0;
    int healthBar = 100;
    int timeLeft = 3000;
    
       /**
     * Act: walk around (using arrows), create plants (using "z")
     * spray water (using space)
     */
    public void act() 
    {
    
        if (Greenfoot.isKeyDown("a")) {
            setRotation(180);
            move(5);
        }
        if (Greenfoot.isKeyDown("w")) {
            setRotation(270);
            move(5);
        }
        if (Greenfoot.isKeyDown("d")) {
            setRotation(0);
            move(5);
        }
        if (Greenfoot.isKeyDown("s")) {
            setRotation(90);
            move(5);
        }        
        if (Greenfoot.isKeyDown("f")){
            World word = getWorld();
            getWorld(World.addObject(new Snowman(), getX(), getY()));
        }
        
        
        // If "z" is pushed, create a plant a the current location
        if (Greenfoot.isKeyDown("z")) {
            World world = getWorld();
           world.addObject(new Plant(), 300, 60);
           world.addObject(new Plant(), 200, 100);
           world.addObject(new Plant(), 250, 150);
        }   
        
        // If spacebar is pushed, create a waterspray at the current location
         if (Greenfoot.isKeyDown("space")) {
        World world = getWorld();
        WaterSpray waterSpray = new WaterSpray();
        waterSpray.setGardener(this);
        world.addObject(waterSpray, getX(), getY());
        
    }
 public void act () {
        move(0);
        if (isTouching (Plant.class)){
        Plant plant = (Plant)getOneIntersectingObject (Plant.class);
        plant.addWater(1);
                gardener.addPoints(1);
        World world = getWorld();
        world.removeObject (this);
       }
 move(0);
        if (isTouching (Plant.class)){
        Plant plant = (Plant)getOneIntersectingObject (Plant.class);
        plant.addWater(1);
        gardener.addPoints(1);
        World world = getWorld();
        world.removeObject (this);
    }
   if (Greenfoot.isKeyDown("space")) {
        World world = getWorld();
        WaterSpray waterSpray = new WaterSpray();
        
        world.addObject(waterSpray, getX(), getY());
        
    }
Lukaas Lukaas

2018/6/21

#
Update: I think the error is somewhere in the isKeyDown (space) of the snowman, the second code is the snowman
Lukaas Lukaas

2018/6/21

#
java.lang.NullPointerException
	at WaterSpray.act(WaterSpray.java:27)
	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)
java.lang.NullPointerException
	at WaterSpray.act(WaterSpray.java:27)
	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)
this is the error i get
danpost danpost

2018/6/21

#
Lukaas wrote...
Update: I think the error is somewhere in the isKeyDown (space) of the snowman, the second code is the snowman
Incorrect. The second line of the error output (the first line of the error trace) indicates the error occurring in your WaterSpray class act method.
Lukaas Lukaas

2018/6/21

#
But what is the error than? I believe it's somewhere in the snowman but it has something to do with the waterspray. When I get rid of the snowman it doesnt give an error
danpost danpost

2018/6/21

#
Lukaas wrote...
But what is the error than? I believe it's somewhere in the snowman but it has something to do with the waterspray. When I get rid of the snowman it doesnt give an error
You need to show the WaterSpray class codes.
Lukaas Lukaas

2018/6/21

#
import greenfoot.*;
import java.awt.Color;

/**
 * A waterspray can be sprayed by the gardener. It helps to give plants water.
 * 
 */
public class WaterSpray extends Actor
{   
    // Field with an objectreference to the gardener that sprayed this water
    Gardener gardener;
   
    /**
     * Set the objectreference to the gardener
     * @param g The objectreference to the gardener
     */
    public void setGardener (Gardener g) {
        gardener = g;
    }    
    
     
    public void act () {
        move(3);
        if (isTouching (Plant.class)){
        Plant plant = (Plant)getOneIntersectingObject (Plant.class);
        plant.addWater(1);
        gardener.addPoints(1);
        World world = getWorld();
        world.removeObject (this);
       }
    }
    }
Lukaas Lukaas

2018/6/21

#
import greenfoot.*;
import java.awt.Color;

/**
 * A waterspray can be sprayed by the gardener. It helps to give plants water.
 * 
 */
public class Snowman extends Actor
{   
    // Field with an objectreference to the gardener that sprayed this water
    Gardener gardener;
   int setSnowman;
    /**
     * Set the objectreference to the gardener
     * @param g The objectreference to the gardener
     */
   
   
     
    public void act () {
        move(0);
        if (isTouching (Plant.class)){
        Plant plant = (Plant)getOneIntersectingObject (Plant.class);
        plant.addWater(1);
                gardener.addPoints(1);
        World world = getWorld();
        world.removeObject (this);
       }
 move(0);
        if (isTouching (Plant.class)){
        Plant plant = (Plant)getOneIntersectingObject (Plant.class);
        plant.addWater(1);
        gardener.addPoints(1);
        World world = getWorld();
        world.removeObject (this);
    }
   if (Greenfoot.isKeyDown("space")) {
        World world = getWorld();
        WaterSpray waterSpray = new WaterSpray();
        
        world.addObject(waterSpray, getX(), getY());
        
    }
}
}
    
      
     
    

danpost danpost

2018/6/21

#
Okay. The first line in the error trace also indicates that the error occurred on line 27. The only thing that could possibly be null on that line is gardener. It is never assigned a snowman (Gardener object). You could just put the following at line 21 in the the act method of the Gardener class (as given in your initial post):
waterSpray.setGardener(this);
but there is a much nicer way. Instead of a public method to set the gardener, you can use a constructor. Change line 17 in the WaterSpray class to:
public WaterSpray(Gardener g) {
Then change line 20 in the act method of the Gardener class (as given in your initial post) to this:
WaterSpray waterSpray = new WaterSpray(this);
Lukaas Lukaas

2018/6/21

#
import greenfoot.*;
import java.awt.Color;

/**
 * A gardener walks around and sprays water for his plants. It's a game, so it 
 * keep tracks of the amount of points. The more points, the better of course.
 * 
 */
public class Gardener extends Actor
{
    // Field for keeping track of the points
    int points = 0;
    int healthBar = 100;
    int timeLeft = 3000;
    
       /**
     * Act: walk around (using arrows), create plants (using "z")
     * spray water (using space)
     */
    public void act() 
    {
    
        if (Greenfoot.isKeyDown("a")) {
            setRotation(180);
            move(5);
        }
        if (Greenfoot.isKeyDown("w")) {
            setRotation(270);
            move(5);
        }
        if (Greenfoot.isKeyDown("d")) {
            setRotation(0);
            move(5);
        }
        if (Greenfoot.isKeyDown("s")) {
            setRotation(90);
            move(5);
        }        
        if (Greenfoot.isKeyDown("f")){
            World word = getWorld();
            getWorld().addObject(new Snowman(), getX(), getY());
        }
        
        
        // If "z" is pushed, create a plant a the current location
        if (Greenfoot.isKeyDown("z")) {
            World world = getWorld();
           world.addObject(new Plant(), 400, 60);
           world.addObject(new Plant(), 200, 100);
           
           world.addObject(new Plant(), 50, 300);
           world.addObject(new Plant(), 450, 350);
        }   
        
        // If spacebar is pushed, create a waterspray at the current location
         if (Greenfoot.isKeyDown("space")) {
        World world = getWorld();
        WaterSpray waterSpray = new WaterSpray(this);
        
        world.addObject(waterSpray, getX(), getY());
        
    }
        // Update the points
        updateText();
    }    
    
    /**
     * Add a certain amount of points (e.g. when the waterspray hits a plant)
     * Make sure the gardener shows the amount of points
     * @param p amount of points to be added
     */
    public void addPoints (int p) {
        points += p;
        updateText();
    }
    
    /**
     * Show the amount of points above the head of the gardener. This is a non-public
     * method (only avaible from inside the class).
     * 
     */
    private void updateText() {
        GreenfootImage newImage = new GreenfootImage ("gardener.png");      
        newImage.setColor (Color.BLACK);
        newImage.drawString(points + "", 10, 15);
        setImage (newImage);
    }
   
}
import greenfoot.*;
import java.awt.Color;

/**
 * A waterspray can be sprayed by the gardener. It helps to give plants water.
 * 
 */
public class Snowman extends Actor
{   
    // Field with an objectreference to the gardener that sprayed this water
    Gardener gardener;
   int setSnowman;
    /**
     * Set the objectreference to the gardener
     * @param g The objectreference to the gardener
     */
   
   
     
    public void act () {
        move(0);
        if (isTouching (Plant.class)){
        Plant plant = (Plant)getOneIntersectingObject (Plant.class);
        plant.addWater(1);
                gardener.addPoints(1);
        World world = getWorld();
        world.removeObject (this);
       }
 move(0);
        if (isTouching (Plant.class)){
        Plant plant = (Plant)getOneIntersectingObject (Plant.class);
        plant.addWater(1);
        gardener.addPoints(1);
        World world = getWorld();
        world.removeObject (this);
    }
   if (Greenfoot.isKeyDown("space")) {
        World world = getWorld();
        WaterSpray waterSpray = new waterSpray();
        
        world.addObject(waterSpray, getX(), getY());
        
    }
}
}
    
      

import greenfoot.*;
import java.awt.Color;

/**
 * A waterspray can be sprayed by the gardener. It helps to give plants water.
 * 
 */
public class WaterSpray extends Actor
{   
    // Field with an objectreference to the gardener that sprayed this water
    Gardener gardener;
   
    /**
     * Set the objectreference to the gardener
     * @param g The objectreference to the gardener
     */
    public WaterSpray(Gardener g) {
        gardener = g;
    }    
    
     
    public void act () {
        move(3);
        if (isTouching (Plant.class)){
        Plant plant = (Plant)getOneIntersectingObject (Plant.class);
        plant.addWater(1);
        gardener.addPoints(1);
        World world = getWorld();
        world.removeObject (this);
       }
    }
    }
Lukaas Lukaas

2018/6/21

#
There is still an error in the snowman class, line 128.
danpost danpost

2018/6/21

#
Lukaas wrote...
There is still an error in the snowman class, line 128.
Again, add this as a parameter in creating a new WaterSpray object.
Lukaas Lukaas

2018/6/21

#
Sorry but I don't know what you mean with that. This is my first time working with greenfoot so :)
danpost danpost

2018/6/21

#
Lukaas wrote...
Sorry but I don't know what you mean with that. This is my first time working with greenfoot so :)
Make same as last line in my previous post.
Lukaas Lukaas

2018/6/21

#
??
danpost wrote...
Lukaas wrote...
Sorry but I don't know what you mean with that. This is my first time working with greenfoot so :)
Make same as last line in my previous post.
There are more replies on the next page.
1
2
3