Search code examples
javaexceptionarrayliststaticremovechild

Exception when removing items from an ArrayList


I've been pondering a solution for quite some time now. Every time I attempt to remove items from an ArrayList, I always end up with an Exception error. items.add() in relation to the ArrayList works perfectly fine, though I can't get my head around as to why it throws an exception when removing items (items.remove()).

This ArrayList is for an inventory, which draws and updates each item. I used the typical for loop to iterate through each object:

for (int i = 0; i < items.size(); i++)

Here's the inside of my Inventory class:

public class Inventory {

public static boolean bOpen;
public static boolean bSelected;

private Rectangle selectBox;
private Color hover;
private Color select;
private Color color;
private Font f;
private FontMetrics fontMetrics;

public static int tileCount;
private boolean pressed;

private int sx;
private int sy;

public boolean I;
private BufferedImage HUD;
private BufferedImage infoBase;
public static ArrayList<Item> items;
private int itemCount;

// Items
public static Shovel shovel;
public static PickAxe pickAxe;
public static Key key;

public Inventory() {
    try {
        HUD = ImageIO.read(getClass().getResource("/HUD.png"));
        infoBase = ImageIO.read(getClass().getResource("/InfoBase.png"));
    } catch (Exception e) {
        e.printStackTrace();
    }

    items = new ArrayList<Item>();

    // Init items
    shovel = new Shovel();
    pickAxe = new PickAxe();
    key = new Key();

    items.add(shovel);
    items.add(pickAxe);

    hover = new Color(150, 150, 180, 180);
    select = new Color(255, 174, 0, 180);

    f = new Font("Bodoni MT", Font.PLAIN, 12);

    sx = 34;
    sy = 198;
}

public void draw(Graphics2D g) {

    itemCount = items.size();

    if (I) {
        bOpen = true;
    }

    if (bOpen) {
        g.setColor(new Color(0, 0, 0, 180));
        g.fillRect(0, 0, GamePanel.WIDTH, GamePanel.HEIGHT);
        g.drawImage(HUD, 26, 194, 270, 41, null);

        for (int i = 0; i < items.size(); i++) {

            if (items.get(i) != null) {
                items.get(i).update();
                items.get(i).draw(g, 22 + i * 32, 180);
            }

            g.setColor(color);
            g.setStroke(new BasicStroke(2));
            selectBox = new Rectangle(sx, sy, 32, 32);

            if (!bSelected && items.get(i) != null) {
                color = hover;
                items.get(tileCount).bSelected = false;

            } else {
                color = select;
                if (items.get(i) != null) {
                    items.get(tileCount).bSelected = true;
                    g.drawImage(infoBase, 20, 59, null);
                    g.setFont(f);
                    fontMetrics = g.getFontMetrics();
                    int width = fontMetrics.stringWidth(items
                            .get(tileCount).getInfo());
                    g.drawString(items.get(tileCount).getInfo(),
                            GamePanel.WIDTH / 4 - width / 2, 80);
                }
            }

            g.draw(selectBox);
        }

        if (GamePanel.right) {
            bSelected = false;
            if (GamePanel.left == false) {
                if (!pressed) {
                    pressed = true;
                    if (tileCount <= itemCount - 2) {
                        try {
                            Thread.sleep(120);
                            sx = sx + 32;
                            tileCount = tileCount + 1;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        if (GamePanel.left) {
            bSelected = false;
            if (GamePanel.right == false) {
                if (!pressed) {
                    pressed = true;
                    if (tileCount > 0) {
                        try {
                            Thread.sleep(120);
                            sx = sx + -32;
                            tileCount = tileCount - 1;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        if (GamePanel.right || GamePanel.left) {
            pressed = false;
        }

        if (GamePanel.enter && itemCount > 0) {
            bSelected = true;
        }
    }

    if (itemCount > 8) {
        items.remove(8);
    }
    System.out.println(items.size());
}

}

Here's the class where I call the items.remove() method:

public class Chest extends Item {

public static boolean bOpen;

private StringEvent se;

public Chest() {
    se = new StringEvent();
    bSolid = true;
}

public void draw(Graphics2D g, int x, int y) {
    setRadius(TileLayer.xx + x + getWidth() / 2, TileLayer.yy + y
            + getHeight() / 2, 32, 32);
    checkCollision();

    if (bTouched) {
        if (GamePanel.e) {
            if (Inventory.key.bSelected == true) {
                bOpen = true;
                Inventory.key.bUsed = true;
            } else
                se.draw(g, this, 2, "Chest is locked.");
        }
    }

    if (!bOpen) {
        g.drawImage(content.chestShut(), TileLayer.xx + x,
                TileLayer.yy + y, null);
    }

    if (bOpen) {
        g.drawImage(content.chestOpen(), TileLayer.xx + x,
                TileLayer.yy + y, null);
        if (Inventory.bOpen == false) {
            Audio.playOnce(Audio.unlock);
        }
    }

    if (Inventory.key.bUsed && !bRemove) {
        Inventory.items.remove(Inventory.key);
        bRemove = true;
    }
}

public void update() {
}

}

Any ideas on how to overcome this problem?

Edit: Here's the stacktrace:

Game (31) [Java Application]    
Main.Game at localhost:49264    
    Thread [AWT-Shutdown] (Running) 
    Daemon Thread [AWT-Windows] (Running)   
    Thread [AWT-EventQueue-0] (Running) 
    Thread [Thread-2] (Suspended (exception IndexOutOfBoundsException)) 
        ArrayList<E>.rangeCheck(int) line: not available    
        ArrayList<E>.get(int) line: not available   
        Inventory.draw(Graphics2D) line: 102    
        GamePanel.draw() line: 184  
        GamePanel.run() line: 105   
        Thread.run() line: not available    
    Thread [DestroyJavaVM] (Running)    
    Daemon Thread [Java Sound Event Dispatcher] (Running)   
    Daemon Thread [Direct Clip] (Running)   
    Daemon Thread [Direct Clip] (Running)   
    Daemon Thread [Direct Clip] (Running)   
    Daemon Thread [Direct Clip] (Running)   
    Daemon Thread [Direct Clip] (Running)   
    Daemon Thread [Direct Clip] (Running)   
    Daemon Thread [Direct Clip] (Running)   
    Daemon Thread [Direct Clip] (Running)   
    Daemon Thread [Direct Clip] (Running)   
C:\Program Files\Java\jre7\bin\javaw.exe (18 Sep 2014 18:40:22) 

Thanks for the help so far.

I also get this in my console:

Exception in thread "Thread-2" 

I tried editing the itemSize to items.size(), but unfortunately an error still occurs. I do not believe that the itemCount is the problem because the items.size() does not exceed the value of '8'.

EDIT: Ok, so I figured out the problem. items.get(i) is causing the Exception becuase of this:

if (!bSelected) {
                color = hover;
                if (items.get(i) != null) {
                    items.get(tileCount).bSelected = false;

            } else {
                color = select;
                if (items.get(i) != null) {
                    items.get(tileCount).bSelected = true;
                    g.drawImage(infoBase, 20, 59, null);
                    g.setFont(f);
                    fontMetrics = g.getFontMetrics();
                    int width = fontMetrics.stringWidth(items
                            .get(tileCount).getInfo());
                    g.drawString(items.get(tileCount).getInfo(),
                            GamePanel.WIDTH / 4 - width / 2, 80);
                }
            }

It's because as SOON as the inventory opens, it tries to set items.get(i).bSelected = true or items.get(i).bSelected = false. Maybe it's trying to find something that doesn't exist in the ArrayList anymore? Still having the same error :/


Solution

  • [FIXED]

    I had

    if (items.get(i) != null) {
                        items.get(tileCount).bSelected = false;
                    }
    

    when the item that's being removed doesn't even exist! I just deleted this statement and it works perfectly fine!