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

2012/10/30

Can't access variable from actor.

Dzifzar Dzifzar

2012/10/30

#
I am trying to make a point counter, and it all compiles but the terminal comes up and says:
java.lang.NullPointerException
	at Score.<init>(Score.java:16)
	at World3.<init>(World3.java:25)
These are the three classes. The first is the actor with the variable, the second is the world, and the third is the scoreboard.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class LinkHero extends Dragon
{

    public int points = 0;
    private int counter = 0;

    public void act() 
    {
     move();
     get();
    }
    
    public void move()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            move(4);
             if (counter <= 2)
            {
                setImage("LR1.png");   
            }
            else if (counter == 4)
            {
                setImage("LR2.png");
            }
            else if (counter == 6)
            {
                setImage("LR3.png");
            }
            else if (counter == 8)
            {
                setImage("LR4.png");
            }
            else if (counter == 10)
            {
                setImage("LR5.png");
            }
            else if (counter == 12)
            {
                setImage("LR6.png");
            }
            else if (counter == 14)
            {
                setImage("LR7.png");
            }
            else if (counter == 16)
            {
                setImage("LR8.png");
            }

            counter++;
            if (counter > 16)
            {
            counter = 0;
            }
        }
        
        if (Greenfoot.isKeyDown("left"))
        {
            setRotation(0);
            move(-4);
             if (counter <= 2)
            {
                setImage("LL1.png");   
            }
            else if (counter == 4)
            {
                setImage("LL2.png");
            }
            else if (counter == 6)
            {
                setImage("LL3.png");
            }
            else if (counter == 8)
            {
                setImage("LL4.png");
            }
            else if (counter == 10)
            {
                setImage("LL5.png");
            }
            else if (counter == 12)
            {
                setImage("LL6.png");
            }
            else if (counter == 14)
            {
                setImage("LL7.png");
            }
            else if (counter == 16)
            {
                setImage("LL8.png");
            }

            counter++;
            if (counter > 16)
            {
            counter = 0;
            }
        }
        
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY() - 4);
            if (counter <= 2)
            {
                setImage("LU1.png");   
            }
            else if (counter == 4)
            {
                setImage("LU2.png");
            }
            else if (counter == 6)
            {
                setImage("LU3.png");
            }
            else if (counter == 8)
            {
                setImage("LU4.png");
            }
            else if (counter == 10)
            {
                setImage("LU5.png");
            }
            else if (counter == 12)
            {
                setImage("LU6.png");
            }
            else if (counter == 14)
            {
            setImage("LU7.png");
            }

            counter++;
            if (counter > 14)
            {
            counter = 0;
            }
        }
        
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY() + 4);
             if (counter <= 2)
            {
                setImage("LD1.png");   
            }
            else if (counter == 4)
            {
                setImage("LD2.png");
            }
            else if (counter == 6)
            {
                setImage("LD3.png");
            }
            else if (counter == 8)
            {
                setImage("LD4.png");
            }
            else if (counter == 10)
            {
                setImage("LD5.png");
            }
            else if (counter == 12)
            {
                setImage("LD6.png");
            }
            else if (counter == 14)
            {
                setImage("LD7.png");
            }

            counter++;
            if (counter > 14)
            {
            counter = 0;
            }
        }
        
        if (Greenfoot.isKeyDown("enter"))
        {
            Greenfoot.playSound("win.wav");
            Greenfoot.stop();
        }
    }
    
    public void get()
    {
        Actor rupee;
        rupee = getOneObjectAtOffset(0, 0, Rupee.class);
        if (rupee != null)
        {
            World world = getWorld();
            world.removeObject(rupee);
            Greenfoot.playSound("rupee.wav");
            points++;
        }
    }
    
    public void Update()
    {
        ;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class World3 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class World3 extends World
{
    public int ScorePoints = 0;
    private LinkHero Hero;
    private Score score;
    
    /**
     * Constructor for objects of class World3.
     * 
     */
    public World3()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(700, 600, 1);
        Hero = new LinkHero();
        addObject(Hero, 358, 330);
        score  = new Score();
        addObject(score, 691, 576);
        
        prepare();
    }
    
        public LinkHero get()
    {
        return Hero;
    }
    
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
       ...(object adding)...
        get();
    }

}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    private int score;
    
    public Score()
    {
        score = ((World3) getWorld()).get().points;
        setImage (new GreenfootImage(200, 30));
        Update();
    }
    
    public void act()
    {
        Update();
    }
    
    public void addScore()
    {
        score++;
        Update();
    }
    
    public void Update()
    {
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(Color.WHITE);
        img.setFont( new Font("Nanum Brush Script", Font.PLAIN, 36));
        img.drawString("0" + score, 50, 20);
    }
}
Any help is much appreciated! Thank you so much!
SPower SPower

2012/10/30

#
    public Score()
    {
        score = ((World3) getWorld()).get().points;
this is the problem. getWorld returns null, and you can't use an object which is null so you get a null pointer exception. To fix it, I recommend moving this code to the addedToWorld method:
protected void addedToWorld(World world)
{
    score = ((World3) getWorld()).get().points;
}
For more about exceptions, check this out or this.
You need to login to post a reply.