0

I'm currently trying to make a little game. The aim is to fly around shooting and collecting items. I've figured out how to shoot how i intend. However, my bullets dont get removed after they go out of bounds, giving me an java.lang.NullPointerException error. Any help would be appreciated!

public class Hero extends Player {

    public int bulletCount = 0;

    public void act() {
        checkFire();
        bulletCount++;
    }

    public void checkFire() {
        if (bulletCount % 10 == 0) {
            bulletCount = 0;

            if (Greenfoot.isKeyDown("space")) {
                int x = getX(), y = getY();
                Bullet bullet = new Bullet(x, y, rotation);               
                getWorld().addObject(bullet, getX(), getY());            
            }
        }
    }
}



public class Bullet extends Player {

    private int speed = 10;

    public Bullet(int x, int y, int rotation) {
        setLocation(x,y);
        setRotation(rotation);
    }

    public void act() {
        move(speed);
        checkBoundaries();
    }

    public void checkBoundaries() {
        if (getX() > getWorld().getWidth() - 10) 
            getWorld().removeObject(this);
        else if (getX() < 10) 
            getWorld().removeObject(this);

        if (getY() > getWorld().getHeight() - 10) 
            getWorld().removeObject(this);
        else if (getY() < 10) 
            getWorld().removeObject(this);
    }
}

public class Enemy extends Actor{

    public void killHero()
{
    Actor hero1 = getOneIntersectingObject(Hero.class);
    if(hero1 != null) {                  
        World world;
        world = getWorld();
        LivesCounter livescounter = new LivesCounter();
        this.setLocation(Greenfoot.getRandomNumber(world.getWidth()), Greenfoot.getRandomNumber(world.getHeight()));
        hero.lives--;
    }
}


public class GameScreen extends World
{

public GameScreen()
{    
    super(600, 400, 1); 
    prepare();
}

private void prepare()
{
    Hero hero = new Hero();
    addObject(hero,114,197);
    Enemy enemy = new Enemy();
    Asteroid asteroid = new Asteroid();
    addObject(asteroid,150,72);
    Girl girl = new Girl();
    addObject(girl,285,72);
    Alien alien = new Alien();
    addObject(alien,503,191);
    Boy boy = new Boy();
    addObject(boy,477,75);
}

}

The bullets can hit and destroy my asteroids but not my aliens for some weird reason but ill figure that out after my bullets disappear. If my bullets hit the world edge error. If im close to the world edge when i shoot i get an error.

Bodrov
  • 840
  • 1
  • 15
  • 29
Ryan W
  • 41
  • 1
  • 7

1 Answers1

0

You cannot call getX() or getY() after you have removed yourself from the world. If a bullet leaves to the left or right, it's still checking for its vertical bounds afterwards. There's two ways to fix this. The simplest is to use else to join your two ifs together:

public void checkBoundaries() {
        if (getX() > getWorld().getWidth() - 10) 
            getWorld().removeObject(this);
        else if (getX() < 10) 
            getWorld().removeObject(this);
        else if (getY() > getWorld().getHeight() - 10)  // <--- Now an else here
            getWorld().removeObject(this);
        else if (getY() < 10) 
            getWorld().removeObject(this);
    }

This avoids calling getY() after removal from the world.

Neil Brown
  • 3,558
  • 1
  • 27
  • 36