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

2013/6/7

Null pointer error

Gingervitis Gingervitis

2013/6/7

#
My game has been working perfectly for more than an hour and then all of a sudden I got a null pointer exception. Here is my code, can someone take a look at it?
public void location()
    {
        setLocation(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY()-25);
    }
JetLennit JetLennit

2013/6/7

#
It looks like the mouse is out of the mini-screen... try having the this as the code
public void location()  
    {  
if(Greenfoot.getMouseInfo() != null)
{
        setLocation(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY()-25);  
}
    }  
Gingervitis Gingervitis

2013/6/7

#
That worked! Thank you! I have one question, what is a mini-screen?
JetLennit JetLennit

2013/6/7

#
I meant the window that greenfoot is in
Gingervitis Gingervitis

2013/6/7

#
oh ok
Gingervitis Gingervitis

2013/6/8

#
I am getting another null pointer error. Can someone look at my code?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Man here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Man extends Animal
{
    private HealthBar healthBar;
    int x;
    int count;
    /**
     * Act - do whatever the Man wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moving();
        count++;
    } 
    
    public Man(HealthBar healthBar)
    {
        healthBar = healthBar;
    }
    
    public void moving()
    {
        x = 1;
        
        
        if (count <= 50)
        {
            move(x);
        }
        
        if (count >= 51)
        {
            move(-x);
        }
        
        if (Greenfoot.getRandomNumber(500) <= 10)
        {
            count = 1;
        }
        
        Actor rd = getOneIntersectingObject(Raindrop.class);  
        if (rd != null) healthBar.subtract(1);  
        
        
    }
}
the error is at line 50
danpost danpost

2013/6/8

#
You need to show the code creating the Man object with the HealthBar object that it uses.
Gingervitis Gingervitis

2013/6/8

#
What do you mean by that? I am using the progress bar that you created but I don't know 100% how to make it work.
danpost danpost

2013/6/8

#
Then, maybe you should show that code.
Gingervitis Gingervitis

2013/6/8

#
The code for the progress bar?
Gingervitis Gingervitis

2013/6/8

#
I have figured out my health bar error.
Gingervitis Gingervitis

2013/6/9

#
I keep getting null pointer errors in my "moving" method in the code below. Can someone please help me with the errors? ( I included the whole class code in case it is necessary to fix the error)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Man here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Man extends Animal
{
    private HealthBar health; 
    private HealthBar charge;
    int count;
    int hpAdd = 125;
    int beamCount = 1; 
    /**
     * Act - do whatever the Man wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moving();
        ifCanSee();
        beam();
        //end();
        count++;
        hpAdd++;
        beamCount++;
    } 
    
    public Man(HealthBar healthBar, HealthBar healthbar2)
    {
        health = healthBar;
        charge = healthbar2;
    }
    
    public void moving()
    {                
        if(  getX() <= Greenfoot.getMouseInfo().getX()  )
        {
            move(-1);
        }
        
        if ( getX() >= Greenfoot.getMouseInfo().getX() )
        {
            move(1);
        }                
    }
    
    public void ifCanSee()
    {
        Actor rd = getOneIntersectingObject(Raindrop.class);  
        if (rd != null) getWorld().removeObject(rd);
        if (rd != null ) health.subtract(1);        
        if (rd != null)  hpAdd = 100;
                
        if (hpAdd >= 125)
        {
            health.add(1);
            hpAdd = 90; 
        }
                
        Actor l1 = getOneIntersectingObject(Lightning.class);
        if (l1 != null) health.subtract(5);
        if (l1 != null) getWorld().removeObject(l1);
               
        Actor l2 = getOneIntersectingObject(Lightning2.class);
        if (l2 != null) health.subtract(5);
        if (l2 != null) getWorld().removeObject(l2);
    }
    
    public void end()
    {
        if (health.getValue() == 0)
        {
            Greenfoot.stop();            
            Label label = new Label("Game Over");
            getWorld().addObject(label, 326, 229);
        }
    }
  
    public void beam()
    {
        if (Greenfoot.isKeyDown("space") && beamCount >=1)
        {
            getWorld().addObject(new Beam(), Greenfoot.getMouseInfo().getX()-4,  Greenfoot.getMouseInfo().getY()+28);
            charge.subtract(2);            
        }
        else
        { 
            charge.add(1);           
            getWorld().removeObjects(getWorld().getObjects(Beam.class));
        }
        
        if (charge.getValue() == 0)
        {
            beamCount = -499;
        }
    }
}
danpost danpost

2013/6/9

#
You should include the entire error message (it has pertinent information as to exactly where the error occurred).
You need to login to post a reply.