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

2013/12/9

java.lang.NullPointerException help

kpdanny kpdanny

2013/12/9

#
Hi, I am new to greenfoot and I am currently getting an error with this. I have created a sceanario in which you control a player and you "eat" goals to get points in a maze. If the player touches a mazewall, he or she will be spawned back at the starting point. This is where the error comes in. After spawning again, if one tries to "eat" a goal to add points the error will happen. This is my terminal window java.lang.NullPointerException at Player.TrytoEat(Player.java:56) at Player.act(Player.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) this is my player code
    private Counter counter;
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    
    public void act() 
    {
       
        CheckKeys();
        
        TrytoEat();
        
    }    
    public Player (Counter count)
    {
        counter = count;
        
    }
     public void CheckKeys()
    {
        if ( Greenfoot.isKeyDown("up"))
        {
            move(-1);
        }

        if ( Greenfoot.isKeyDown("down"))
        {
            move(2);
        }
        if ( Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }

        if ( Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
    }
    public void TrytoEat()
    { if(canSee(Goal.class))
        {
            eat(Goal.class);
            counter.add(2);
           if (counter.getValue()> 8)
            {
                gameOver();

            }
        }
       
        
    }
    public void gameOver()
    {
        Greenfoot.stop();
    }

}

this is my mazewall that respawns the player if he touches the wall
  private Counter counter;
    /**
     * Act - do whatever the MazeWall wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        TrytoEatHuman();
        
      
    }    

    public void TrytoEatHuman()
    {
        if (canSee(Player.class))

        {  
            eat(Player.class);
            addPlayer();
         
        }

    }
    public void gameOver()
    {
        Greenfoot.stop();
    }
    public void addPlayer()
        {
           Player player = new Player(counter);
            World world = getWorld();
            int x = 34;
            int y = 42;
            world.addObject(player, x, y);
            
}
}
danpost danpost

2013/12/9

#
I do not believe that you set the Counter counter field in you MazeWall class; so you are passing a 'null' value for the Counter counter field in the Player class.
kpdanny kpdanny

2013/12/9

#
how would I set the Counter counter field in my Mazewall class? Sorry I am new
danpost danpost

2013/12/9

#
Actually, it would be easier just to reset the location of the player; instead of creating a new one. Then you would not have to worry about the counter being set to null.
kpdanny kpdanny

2013/12/9

#
how would one reset the location? would i use setLocation();?
danpost danpost

2013/12/9

#
You would need to get a reference to the player object that the MazeWall 'canSee'.
Actor player = getOneIntersectingObject(Player.class);
would get the reference to the player provided that the MazeWall 'canSee' it. Then, 'player.setLocation(34, 42);' would reset its location. With this, you will not need the 'addPlayer' method.
kpdanny kpdanny

2013/12/9

#
would i post this code in the TrytoEatHuman method?
kpdanny kpdanny

2013/12/9

#
nevermind i figured it out! Thanks for the help I appreciate it!
danpost danpost

2013/12/9

#
Yes.
public void TrytoEatHuman()
{
    Actor player = getOneIntersectingObject(Player.class);
    if (player != null) player.setLocation(34, 42);
}
You need to login to post a reply.