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

2013/6/10

Null Pointer Exception

programmingnewb programmingnewb

2013/6/10

#
Hi! I know what a null pointer exception is, but I don't know why my code is getting that error. Please help!! Basically, the cat class steals jewels from the thief class and assigns the jewel a ran
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Thief here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Thief extends Actor
{
    public int jewelCount = 0;
    private int speed = 3;
    private int health = 3;
    int currentlvl;

    boolean isCaught = false;
    boolean hasLost = false;

    private GreenfootImage rR = new GreenfootImage("robberRight.png");
    private GreenfootImage rL = new GreenfootImage("robberLeft.png");

    public Thief(int a)
    {
        currentlvl = a;
    }

    /**
     * Act - do whatever the Thief wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public void act() 
    {
        setImage(rR);
        go();
        ladder();
        collectJewel();
        nextLvl();
        getCaught();
        loseHealth();
        die();
    }    

    public void go()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            setImage(rR);
            setLocation(getX() + speed, getY());
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setImage(rL);
            setLocation(getX() - speed, getY());
        }
    }

    public void eatCoin()
    {
        Actor coin;
        coin = getOneObjectAtOffset(0, 0, Coin.class);
        if (coin != null)
        {
            World world;
            world = getWorld();
            world.removeObject(coin);
        }
    }

    public boolean onPlatform()
    {
        Actor below = getOneObjectAtOffset (0, getImage().getHeight()/2, Platform.class);  
        return below != null; 
    }

    public void collectJewel()
    {
        Actor j = getOneObjectAtOffset(0, 0, Jewel.class);   
        if (j != null)
        {
            jewelCount ++;
            getWorld().removeObject(j);        
        }
    }
    
    public int getJewelCount()
    {
        return jewelCount;
    }

    public void die()
    {
        if (health == 0)
        {
            Greenfoot.setWorld(new GameOver());
        }
    }

    public void ladder()
    {
        Actor ladder;
        ladder = getOneObjectAtOffset(0, 5, Ladder.class);
        if (ladder != null)
        {
            if (Greenfoot.isKeyDown("up"))
            {
                setLocation(getX(), getY() - 3);
            }
        }
        Actor ladderd;
        ladderd = getOneObjectAtOffset(0, 42, Ladder.class);
        if (ladderd != null)
        {
            if (Greenfoot.isKeyDown("down"))
            { 
                setLocation(getX(), getY() + 3);             
            }
        }
    }

    public boolean getCaught()
    {
        Actor po;
        po = getOneObjectAtOffset(0, 0, PoliceOfficer.class);
        if (po != null)
        {
            isCaught = true;
        }
        return isCaught;
    }

    public void loseHealth()
    {
        if (isCaught)
        {
            health--;
            restartlvl();
        }
    }

    public boolean touchDoor()
    {
        Actor door = getOneObjectAtOffset(-5, 0, Door.class);
        if (door != null)
        {
            return true;
        }
        return false;
    }

    public void nextLvl()
    {
        if (jewelCount == 3 && touchDoor())
        {
            switch (currentlvl)
            {
                case 0:
                Greenfoot.setWorld(new StartMenu());
                break;

                case 1:
                Greenfoot.setWorld(new lvl_2());
                break;

                case 2:
                Greenfoot.setWorld(new lvl_3());
                break;

                case 3:
                Greenfoot.setWorld(new lvl_4());
                break;

                case 4:
                Greenfoot.setWorld(new lvl_5());
                break;

            }
        }
    }

    public void restartlvl()
    {
        if (getHealth() != 0)
        {
            switch (currentlvl)
            {
                case 0:
                Greenfoot.setWorld(new StartMenu());
                health = getHealth();
                break;

                case 1:
                Greenfoot.setWorld(new lvl_1());
                health = getHealth();
                break;

                case 2:
                Greenfoot.setWorld(new lvl_2());
                health = getHealth();
                break;

                case 3:
                Greenfoot.setWorld(new lvl_3());
                health = getHealth();
                break;

                case 4:                
                Greenfoot.setWorld(new lvl_4());
                health = getHealth();
                break;

                case 5:                
                Greenfoot.setWorld(new lvl_5());
                health = getHealth();
                break;
            }
        }
        else
        {
            die();
        }
    }

    public int getHealth()
    {
        return health;
    }
}
cation.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cat here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cat extends Enemy
{
    
    GreenfootImage catR = new GreenfootImage("catRight.png");
    GreenfootImage catL = new GreenfootImage("catLeft.png");
    
    private int r = Greenfoot.getRandomNumber(10);
    private Thief theThief;
    private Jewel theJewel;
    /**
     * Act - do whatever the Cat wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        go();
        if (isTouching(Thief.class))
        {
            stealJewel();
        }
    }   
    
    public void go()
    {
        if (r >= 5)
        {
            goRight();
        }
        if (r <= 4)
        {
            goLeft();
        }
    }

    public void goLeft()
    {
        setImage(catL);
        setLocation(getX() - 2, getY());
        if (getX() <= 20)
        {
            r = 5;
        }
    }

    public void goRight()
    {
        setImage(catR);
        setLocation(getX() + 2, getY());
        if (getX() >= 780)
        {
            r = 4;
        }
    }
    
    public void stealJewel()
    {

        Thief thief = getThief();
        Jewel jewel = getJewel();
        int num = thief.getJewelCount();
        num --; 
        int x = Greenfoot.getRandomNumber(800);
        int y = Greenfoot.getRandomNumber(600);
        jewel.setLocation(x, y);
    }
    
     public Thief getThief()
    {
        return theThief;
    }
    
    public Jewel getJewel()
    {
        return theJewel;
    }
}
Zamoht Zamoht

2013/6/10

#
Which line is the crash referring to?
Gevater_Tod4711 Gevater_Tod4711

2013/6/10

#
I think the problem is in your Thief class. When you get to a new level you set the world to a new world but after this you still execute some methods for which the actor has to be in a world. I think if you put the nextLvl() method (l. 38) at the end of the act method it should work. If this is not the cause for the exception it would be very helpfull to know where this error occours (in which line or in which method). When the stack trace of the exception is printed on the screen there should be a link that tells you where the exception comes from.
You need to login to post a reply.