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

2020/10/17

java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0

DatHeroAndy DatHeroAndy

2020/10/17

#
When I create the variable in line 34 I get this error: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
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;
        }
    }
}
How can I fix that?
danpost danpost

2020/10/18

#
DatHeroAndy wrote...
When I create the variable in line 34 I get this error: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 << Code Omitted >>
It is trying to tell you that the returned list is empty and therefore you cannot get any element from it. First check to ensure the list is NOT empty before trying to get an element from it.
DatHeroAndy DatHeroAndy

2020/10/18

#
danpost wrote...
DatHeroAndy wrote...
When I create the variable in line 34 I get this error: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 << Code Omitted >>
It is trying to tell you that the returned list is empty and therefore you cannot get any element from it. First check to ensure the list is NOT empty before trying to get an element from it.
Fixed it. Thanks for the explanation.
You need to login to post a reply.