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

2013/3/8

Help with error

henrYoda henrYoda

2013/3/8

#
Hi guys I have a problem with the following code, and I am not sure how to fix it
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

public class Key extends Actor
{
private int wait = 0;  
private final int waitTime = 35;  
private boolean touchingPlayer = false;

   /**
     * Create a new key.
     */
public void act()
{
    movementAi();
    die();
    hitPlayer();
    checkTouchingStatus();
   }
  private void checkTouchingStatus()  
     {  
    if (wait > 0) wait--;  
    if (wait == 0) touchingPlayer = false;
}
    //movement  
  
  public void movementAi() 
  {
      
    move(2);
    if(Greenfoot.getRandomNumber(100) < 10)
    {
        turn(Greenfoot.getRandomNumber(90));   
    
    }
    if (getX() <= 5 || getX() >= getWorld().getWidth() - 5)
    {
        turn(180);
    
    }
    if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
    {
       turn(180);
    
    }
}
//If health is 0
    public void die()
    {
    if (getHealth() == 0)
    {
        
        menu menuWorld = (menu) getWorld();
        Bar scoreBar = menuWorld.getScoreBarValue();
        scoreBar.add(50);
        getWorld().removeObject(this);
        
    }
    }

//Enemy health
    private int health = 3;//or any other starting value;  
      
    public void setHealth(int points) {  
        health += points;  
    }  
    public int getHealth() {  
        return health;  
    }  
    
         public void hitPlayer() {  
    key2 key2 = (key2) getOneObjectAtOffset(0, 0, key2.class);  
    if (key2 != null && !touchingPlayer) {  
          menu menuWorld = (menu) getWorld(); //Do this every time to call method!!!
          Bar healthBar = menuWorld.getHealthBarValue();
          healthBar.subtract(1);//decrements the health of your player
          touchingPlayer = true;  
            wait += waitTime;
        
        
      
    

}
   
          
        
          
}
}


    
Does anyone know how to fix this? The error I get is : java.lang.NullPointerException at Key.die(Key.java:55) at Key.act(Key.java:16) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
danpost danpost

2013/3/8

#
It appears that you have edited the code after this error message was given. Please create the error again and post both the error message and the code again befoe making any changes.
henrYoda henrYoda

2013/3/9

#
No, even with the code above I still get the same error for some reason
danpost danpost

2013/3/9

#
Please copy/paste the error message you get with the above code.
henrYoda henrYoda

2013/3/9

#
Ok, this is the error I get with the above code: java.lang.NullPointerException at Key.die(Key.java:54) at Key.act(Key.java:15) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) And I only get this after I kill this Actor (Key)
danpost danpost

2013/3/9

#
That looks better (the act method call to 'die' is in line 15, not 16; and the error occurred on line 54, not 55). Line 53 appears to be bringing in a Bar object (not a value like the method suggests). If the 'getScoreBarValue' method has a return type of 'Bar' then it is possible that, if the bar object is not in the world, it will return 'null'. So, either you forgot to add the bar object for the score into the world or you are not avoiding running this code even when the score bar is not in the world.
henrYoda henrYoda

2013/3/9

#
Oh ok i see thank you
You need to login to post a reply.