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

2013/1/10

Run Time error

BradH BradH

2013/1/10

#
I keep getting a run time error that says that there is a nullpoint error at if(snipercounter.getValue() >= 1) can I not use that many if statements should I split the if statements up?

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Sniper here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Sniper extends Weapons
{  

    private Spacemarinemodel1 spacemarinemodel1; 
    public Sniper(Spacemarinemodel1 spacemarinemodel1) {   
        this.spacemarinemodel1 = spacemarinemodel1;   
    }  

    private SniperCounter snipercounter;
    public Sniper (SniperCounter pointSniperCounter)
    {
        snipercounter = pointSniperCounter;
    }

    /**
     * Act - do whatever the Sniper wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

        

        int spacemarinemodel1X = spacemarinemodel1.getX();   
        int spacemarinemodel1Y = spacemarinemodel1.getY();   
        // Modify the xOffset and   
        // yOffset to make the gun   
        // appear in the correct   
        // position.   
        int xOffset = 27;   
        int yOffset = -15;   
        int x = spacemarinemodel1X + xOffset;   
        int y = spacemarinemodel1Y + yOffset;   
        setLocation(x, y);  

        //switch weapon to Gun key is #1
        if((Greenfoot.isKeyDown("1")))
            getWorld().addObject(new Gun(spacemarinemodel1), getX(), getY());
        //remove the first weapon 
        if((Greenfoot.isKeyDown("1")))
        { getWorld().removeObject(this);
        }
        
        //this key is L
        if((Greenfoot.isKeyDown("l")))
        {
            if(getWorld().getObjects(SniperBullet.class).size()< 1)  
            {
            if(snipercounter.getValue() >= 1)
                {
                    snipercounter.subtract(1);   

                    fire();

                }
            }
        }
        

    }

    private void fire()
    {  //turning around
        Ammo SniperBullet = new SniperBullet();
        getWorld().addObject(SniperBullet, getX() , getY());

        SniperBullet.move(45);

    }
}    


danpost danpost

2013/1/10

#
You have two constructors for the Sniper class. One brings in a SniperCounter object;, while the other brings in a Spacemarinemodel1 object. Which ever one you do use to create a Sniper object, you will not have one the the two objects available to that object. In order for both objects to be avilable, you need a constructor with two parameters, one for each object (combine the two constructors you have into one constructor that does together what each are doing seperately). You will have to adjust you 'new Sniper(....)' calling statement(s) to match.
vonmeth vonmeth

2013/1/10

#
You have two constructors:
 private Spacemarinemodel1 spacemarinemodel1;   
    public Sniper(Spacemarinemodel1 spacemarinemodel1) {     
        this.spacemarinemodel1 = spacemarinemodel1;     
    }    
  
    private SniperCounter snipercounter;  
    public Sniper (SniperCounter pointSniperCounter)  
    {  
        snipercounter = pointSniperCounter;  
    }  
If you create a Sniper with the first constructor, your snipercounter is going to be null. This would cause that error. Maybe you should just have one constructor with both Snipercounter and Spacemarinemodel1. You would have to pass off a Snipercounter and Spacemarinemodel1 object each time you created a Sniper though, but you want both anyways. Since you are switching between Gun and Sniper (it seems), you would probably have to do the same for your Gun class, so that you have a constant reference to Snipercounter (like you have already with a constant reference to your Spacemarinemodel1).
    private Spacemarinemodel1 spacemarinemodel1;
    private SniperCounter snipercounter;
  
    public Sniper(Spacemarinemodel1 spacemarinemodel1, SniperCounter pointSniperCounter) {     
        this.spacemarinemodel1 = spacemarinemodel1;
        snipercounter = pointSniperCounter;  
    }    
BradH BradH

2013/1/10

#
Now if I have this actor Sniper being called in another class would I just define it as Sniper (spacemarinemodel1, snipercounter).
danpost danpost

2013/1/10

#
If you are referring to creating the (new) Sniper object, then yes:
Spacemarinemodel1 spacemarinemodel1 = new Spacemarinemodel1();
Counter snipercounter = new Counter();
// with the above created as shown
Sniper sniper = new Sniper(spacemarinemodel1, snipercounter);
BradH BradH

2013/1/10

#
yes I was, Thanks guys.
You need to login to post a reply.