When I create the variable in line 34 I get this error: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
How can I fix that?
import lang.stride.*;
import java.util.*;
import greenfoot.*;
/**
*
*/
public class Crosshair extends Actor
{
private int gunReloadTimer = 999999999;
private boolean gunIsEmpty = false;
/**
*
*/
public void act()
{
MouseInfo followMouse = Greenfoot.getMouseInfo();
if (followMouse != null) {
setLocation(followMouse.getX(), followMouse.getY());
}
this.gunShooting();
}
/**
*
*/
public void gunShooting()
{
Sky sky = (Sky)getWorld();
if (this.gunReloadTimer >= 1) {
this.gunReloadTimer = this.gunReloadTimer - 1;
}
GunBarrel gb = getWorld().getObjects(GunBarrel.class).get(0); //<--- ERROR
if (Greenfoot.mousePressed(this) && ! this.isTouching(Star.class) && this.gunIsEmpty != true) {
Greenfoot.playSound("shot.mp3");
sky.shotsMissed = sky.shotsMissed + 1;
this.gunReloadTimer = 75;
this.gunIsEmpty = true;
this.setImage("transper.png");
if (gb != null) {
gb.setImage("transper.png");
}
}
if (Greenfoot.mousePressed(this) && this.isTouching(Star.class) && this.gunIsEmpty != true) {
Greenfoot.playSound("shot.mp3");
this.removeTouching(Star.class);
sky.starsShot = sky.starsShot + 1;
this.gunReloadTimer = 75;
this.gunIsEmpty = true;
this.setImage("transper.png");
if (gb != null) {
gb.setImage("transper.png");
}
}
if (this.gunReloadTimer == 0 && this.gunIsEmpty == true) {
Greenfoot.playSound("reload.mp3");
this.setImage("crosshair.png");
this.gunIsEmpty = false;
this.gunReloadTimer = 999999999;
if (gb != null) {
gb.setImage("gunbarrel.png");
}
}
if (this.gunReloadTimer == 0 && this.gunIsEmpty == false) {
this.gunReloadTimer = 999999999;
}
}
}

