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

2014/1/13

my scenario crashes

sanoy sanoy

2014/1/13

#
my scenario crashes it says the mistake is somewhere in this
/**
     * Here you can tell your actor what he has to do.
     */
    public void act() 
    {
        checkKeyPress();   
        if(((desert)getWorld()).start == 1)
        {
            die();
        }
        if(((desert)getWorld()).start == 2)
        {
            checkShotRock();
        }
    }
    
    public void checkShotRock()
    {
        if(canSee(shotRock.class))
        {
            getWorld().removeObject(this);
            System.out.println("You got hit by a rock!");
            System.out.println("You lose!");
            restart();
        }
        if(canSee(octorok.class))
        {
            getWorld().removeObject(this);
            System.out.println("You got hit by an octorok!");
            System.out.println("You lose!");
            restart();
        }
    }
    
    public void restart()
    {
        ((desert)getWorld()).maxScore2 = timeCounter.mins;
        ((desert)getWorld()).start = 0;
    }
the error code is java.lang.NullPointerException at Link.restart(Link.java:59) at Link.checkShotRock(Link.java:52) at Link.act(Link.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:593) at greenfoot.core.Simulation.runOneLoop(Simulation.java:551) at greenfoot.core.Simulation.runContent(Simulation.java:217) at greenfoot.core.Simulation.run(Simulation.java:207) im not good at error code analysing what is wrong
sanoy sanoy

2014/1/13

#
the lines are displaced it means Link.restart(line 35) Link.checkShotRock(line 28) Link.act(line 10)
danpost danpost

2014/1/13

#
If you would show the entire class code, it would be a lot easier to see what is happening.
sanoy sanoy

2014/1/13

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * This class is just an example. You can delete it or change the code.
 * It's not necessary for the scrolling system.
 */
public class Link extends Actor
{  
    public int direction;
    public static int life = 6;
    public static int slimesKilled = 0;
    public int counter = 0;
    public int counter2 = 0;
    public int counter3 = 0;
    public int counter4 = 0;
    public int counter5 = 0;
    public Link()
    {
 
    }
    
    /**
     * Here you can tell your actor what he has to do.
     */
    public void act() 
    {
        checkKeyPress();   
        if(((desert)getWorld()).start == 1)
        {
            die();
        }
        if(((desert)getWorld()).start == 2)
        {
            checkShotRock();
        }
    }
    
    public void checkShotRock()
    {
        if(canSee(shotRock.class))
        {
            getWorld().removeObject(this);
            System.out.println("You got hit by a rock!");
            System.out.println("You lose!");
            restart();
        }
        if(canSee(octorok.class))
        {
            getWorld().removeObject(this);
            System.out.println("You got hit by an octorok!");
            System.out.println("You lose!");
            restart();
        }
    }
    
    public void restart()
    {
        ((desert)getWorld()).maxScore2 = timeCounter.mins;
        ((desert)getWorld()).start = 0;
    }
    
    public void die()
    {
        if (life == 0)
        {
            getWorld().removeObject(this);
            System.out.println("You died!");
            System.out.println("Now the monsters will capture Zelda");
        }   
    }
    
    public boolean space()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            return true;
        }
        else 
        {
            return false;
        }
    }

    public void checkKeyPress()
    {
        if (Greenfoot.isKeyDown("up")) {
                setLocation(getX(), getY() - 3);
                octorok.y = octorok.y -3;
                if(space() == false)
                {
                    setImage("player4.png");
                    direction = 1;
                }
        }
        else
        if (Greenfoot.isKeyDown("down")) {
                setLocation(getX(), getY() + 3);
                octorok.y = octorok.y +3;
                if(space() == false)
                {
                    setImage("player3.png");
                    direction = 2;
                }
        }
        else
        if (Greenfoot.isKeyDown("left")) {
                setLocation(getX() - 3, getY());
                octorok.x = octorok.x -3;
                if(space() == false)
                {
                    setImage("player2.png");               
                    direction = 3;
                }
        }
        else
        if (Greenfoot.isKeyDown("right")) {
                setLocation(getX() + 3, getY());
                octorok.x = octorok.x +3;
                if(space() == false)
                {
                    setImage("player1.png");
                    direction = 4;
                }
        }
        if(space() == false)
                    {
            if(direction == 1)
            {
                setImage("player4.png");
            }
            if(direction == 2)
            {
                setImage("player3.png");
            }
            if(direction == 3)
            {
                setImage("player2.png");             
            }
            if(direction == 4)
            {
                setImage("player1.png");
            }
        }
        if(space() == true)
        {
            if(((desert)getWorld()).start == 1)
            {
                if(direction == 1)
                {
                    setImage("player8.png");
                }
                if(direction == 2)
                {
                    setImage("player7.png");
                }
                if(direction == 3)
                {
                    setImage("player6.png");             
                }
                if(direction == 4)
                {
                    setImage("player5.png");
                }   
            }
        }
        if(space() == true)
        {
            if(direction == 1)
            {
                Actor slime = getOneObjectAtOffset(0,-(2+getImage().getWidth()/2), slime.class);  
                if (slime != null)  
                {  
                    getWorld().removeObject(slime);  
                    slimesKilled++;  
                }  
            }
            if(direction == 2)
            {
                Actor slime = getOneObjectAtOffset(0,(2+getImage().getWidth()/2),slime.class);  
                if (slime != null)  
                {  
                    getWorld().removeObject(slime);  
                    slimesKilled++;  
                }  
            }
            if(direction == 3)
            {
                Actor slime = getOneObjectAtOffset(-(2+getImage().getWidth()/2), 0, slime.class);  
                if (slime != null)  
                {  
                    getWorld().removeObject(slime);  
                    slimesKilled++;  
                }     
            }
            if(direction == 4)
            {
                Actor slime = getOneObjectAtOffset((2+getImage().getWidth()/2), 0, slime.class);  
                if (slime != null)  
                {  
                    getWorld().removeObject(slime);  
                    slimesKilled++;  
                }  
            }
        }
        if(space()==false)
        {
            if (counter == 0)
            {
                if(canSee(slime.class))
                {
                    life = life - 1;
                    counter = 1;
                }
            }
        }
        if(counter == 1)
        {
            if(counter4 ==10)
            {
                if (counter2 == 0)
                {
                    setImage("nothing.png");
                    counter2 = 1;
                    counter5 = 1;
                }
                if(counter2 == 1)
                {
                    if(counter5 == 0)
                    {
                        setImage("player1.png");
                        counter2 = 0;
                        counter3 = counter3 + 1;
                    }
                }
                if(counter3 == 5)
                {
                    counter2 = 0;
                    counter = 0;
                    counter3 = 0;
                }
                counter4 = 0;
                counter5 = 0;
            }
            counter4 = counter4 +1;
        }
    }
      public boolean canSee(Class clss)
    {
          Actor actor = getOneObjectAtOffset(0,0, clss);
        return actor != null;        
    }
}
its the same scenario as in my change background question if the actor link can see oktorok or shotRock it should resart, but it crashes
danpost danpost

2014/1/13

#
What through me off was it said line 59 (not line 58 where the error should be occurring with this code). What is happening is you are calling 'restart' after removing 'this' from the world and that method needs 'this' to be in the world so that you have a world returned by 'getWorld' that the fields are located in. In other worlds because 'this' Link object is no longer in the world, 'getWorld' in the 'restart' method will return a 'null' value and you cannot call a method on or access fields from a 'null' object.
You need to login to post a reply.