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

2013/12/5

java.lang.NullPointerExpection

joe.colo1 joe.colo1

2013/12/5

#
I am having an issue where the code I have is saying that there is a java.lang.NullPointerExpection. Could you please help
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Turtleworld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Turtleworld extends World
{
    public int TurtlesNibbled = 0;
    private Swag swag;
    /**
     * Constructor for objects of class Turtleworld.
     * 
     */
    public Turtleworld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        populateWorld();
        reLive();
    }
    public void reLive()
    {
        swag.getScore();
        if (TurtlesNibbled == 1)
        {
            
            addObject(new Turtle(), 300, 200 );
            
        }
        
    }
    public void populateWorld()
    {
      addObject(new Turtle(), 300, 200 );
    }
}
bourne bourne

2013/12/5

#
You never initialize the variable "swag" And for any Exception, please indicate what line the Exception came from (makes helping you a lot easier)
joe.colo1 joe.colo1

2013/12/5

#
"swag" was a class, sorry about using swag it would not allow me to create a different name. where would i initalize it?
joe.colo1 joe.colo1

2013/12/5

#
line 25 and line 21
bourne bourne

2013/12/5

#
Depends on context. But make sure it is initialized before trying to invoke it (calling methods on it). private Swag swag = new Swag(); // Or whatever constructors you have for that class
You need to login to post a reply.