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

2015/9/20

java.lang.NullPointerException error

ZoSo ZoSo

2015/9/20

#
when i defeat my boss i get this error: java.lang.NullPointerException at Alien.act(Alien.java:41) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) here is where it says the code is wrong, on line 33:
import greenfoot.*;

/**
 * Write a description of class Alien here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Alien extends Actor
{
    GifImage gif = new GifImage("alien.gif");

    private int timer = 5;
    private Score myScore;
    private Counter myCounter;

    public Alien(Score scoreField, Counter counter)
    {
        this.myScore = scoreField;
        myCounter = counter;

    }

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

        setImage(gif.getCurrentImage());

        if (myCounter.getValue()<=0)
        {

            setLocation(getWorld().getWidth()+50, Greenfoot.getRandomNumber(400));
            return;
        }

        setLocation(getX()-7, getY());
        if (getX()<=0)
        {
            setLocation(getWorld().getWidth()+50, Greenfoot.getRandomNumber(400));

        }

        killEnemy();


    }

    public void killEnemy()
    {
        if (getOneIntersectingObject(Missile.class)!=null)
        {
            getWorld().addObject(new Explosion(), getX(), getY());
            Greenfoot.playSound("explosion.wav");
            Actor missile = getOneIntersectingObject(Missile.class);
            getWorld().removeObject(missile);
            myScore.add(1);

            if (myScore.getScore() > 5)
            {
                Boss boss = new Boss(myScore);
                World world = getWorld();
                world.addObject(boss, world.getWidth()+20, Greenfoot.getRandomNumber(world.getHeight()));
                Greenfoot.playSound("tff.wav");

                world.removeObject(this);
                return;
            }

            resurrect();

        }
    }

    public void resurrect()
    {

        setLocation(getWorld().getWidth()+50, Greenfoot.getRandomNumber(400));

    }
}
here is the boss class:
import greenfoot.*;

/**
 * Write a description of class Boss here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boss extends Actor
{
    //GifImage gif = new GifImage("boss.gif");

    private int velX = -4;
    private int velY = 4;

    private int fireTimer = 70;

    private int hits = 0;
    
    //myScore
    private Score myScore;
    private Counter myCounter;
    
    public Boss(Score myScore) // boss4 is fooking amazing
    {
        GreenfootImage image = getImage();
        image.scale(275,380);
        setImage(image);
        
        this.myScore = myScore;
    }

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

        moveShip();
        killBoss();

        if (fireTimer > 0)
        {
            fireTimer--;
        }
        else
        {
            EnemyLaser e1 = new EnemyLaser();
            getWorld().addObject(e1, getX(), getY());
            fireTimer = Greenfoot.getRandomNumber(50)+25;
        }
    }   

    public void moveShip()
    {
        if (getX() < 600) velX = -velX; 
        if (getX() > 900) velX = -velX; 
        if (getY() < 0) velY = -velY; 
        if (getY() > 600) velY = -velY; 

        setLocation(getX() + velX, getY() + velY);
    }

    public void killBoss()
    {
        if (getOneIntersectingObject(Missile.class)!=null)
        {
            getWorld().addObject(new Explosion(), getX(), getY());
            Greenfoot.playSound("explosion.wav");
            Actor missile = getOneIntersectingObject(Missile.class);
            getWorld().removeObject(missile);

            hits++;
            if (hits > 5)
            {
                getWorld().addObject(new Explosion(), getX(), getY());
                Greenfoot.playSound("explosion.wav");
                
                
                myScore.add(10);
                
                Alien a1 = new Alien(myScore, myCounter);
                World world = getWorld();
                world.addObject(a1, world.getWidth()+20, Greenfoot.getRandomNumber(world.getHeight()));
                
                getWorld().removeObject(this);
                return;
            }

        }
    }
}
here is my player class:
import greenfoot.*;

/**
 * Write a description of class PlayerShip here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayerShip extends Actor
{
    GifImage gif = new GifImage("Bunchie.gif");

    private int canShoot=0;
    private int lives = 3;

    private Counter myCounter;
    public PlayerShip(Counter counter)
    {
        myCounter = counter;
        myCounter.setValue(lives);

    }

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

        moveUp();
        moveDown();
        moveLeft();
        moveRight();
        fire();
        canShoot--;

        damage();
    }

    private void moveUp()
    {
        if (Greenfoot.isKeyDown("w"))
        {       
            setLocation(getX(),getY()-4);
        }
    }

    private void moveDown()
    {
        if (Greenfoot.isKeyDown("s"))
        {       
            setLocation(getX(),getY()+4);
        }
    }

    private void moveLeft()
    {
        if (Greenfoot.isKeyDown("a"))
        {       
            setLocation(getX()-4,getY());
        }
    }

    private void moveRight()
    {
        if (Greenfoot.isKeyDown("d"))
        {       
            setLocation(getX()+4,getY());
        }
    }

    public void fire()
    {
        if (Greenfoot.isKeyDown("space")&&(canShoot<=0))
        {
            getWorld().addObject(new Missile(), getX(), getY());
            Greenfoot.playSound("laser.wav");
            canShoot = 50;
        }

        if (Greenfoot.isKeyDown("shift"))
        {
            getWorld().addObject(new Missile(), getX(), getY());
            Greenfoot.playSound("laser.wav");

        }
    }

    public void damage()
    {

        if (getOneIntersectingObject(Alien.class) !=null)
        {

            getWorld().addObject(new Explosion(), getX(), getY());
            Greenfoot.playSound("explosion.wav");
            Actor alien = getOneIntersectingObject(Alien.class);
            alien.setLocation(getWorld().getWidth()+50, Greenfoot.getRandomNumber(400));
            myCounter.add(-1);
            if (myCounter.getValue()>0)
            {
                resurrect();
            }
            else
            {
                getWorld().removeObject(this);
                return;
            }
        }
        
        if (getOneIntersectingObject(EnemyLaser.class) !=null)
        {

            getWorld().addObject(new Explosion(), getX(), getY());
            Greenfoot.playSound("explosion.wav");
            Actor enemylaser = getOneIntersectingObject(EnemyLaser.class);
            getWorld().removeObject(enemylaser);
            myCounter.add(-1);
            if (myCounter.getValue()>0)
            {
                resurrect();
            }
            else
            {
                getWorld().removeObject(this);
            }

        }
    }

    public void resurrect()
    {

        setLocation(100,200);

    }
}
danpost danpost

2015/9/20

#
When you create a Boss object, you are not setting 'myCounter' in the Boss class to anything (it remains 'null'). So, when you re-create a new Alien object from the Boss class and pass 'myCounter', the new Alien object set its 'myCounter' to 'null'.
ZoSo ZoSo

2015/9/20

#
ohhhhhhhh i see, thank you. so all i have to do is set the counter in Boss to something, right?
danpost danpost

2015/9/20

#
ZoSo wrote...
ohhhhhhhh i see, thank you. so all i have to do is set the counter in Boss to something, right?
That would do. However, you would probably want to set it to the same Counter object the initial Alien had. You could pass it from the Alien object that creates it. Better, would be to have the Counter object referenced in your World subclass and not pass it around as you are doing. You can add a getter method for you actors to gain access to that Counter object. Refer to the greenfoot tutorials on how this is done.
ZoSo ZoSo

2015/9/20

#
alright, and one more question, would making the Boss class a sub class of the Alien class work too or no?
danpost danpost

2015/9/20

#
ZoSo wrote...
alright, and one more question, would making the Boss class a sub class of the Alien class work too or no?
No -- absolutely not. A boss and an alien each have their own behavior; and, anyways, a boss is not an alien (as least, not as you describe one).
You need to login to post a reply.