Java 从ArrayList中删除项时出现异常

Java 从ArrayList中删除项时出现异常,java,exception,arraylist,static,removechild,Java,Exception,Arraylist,Static,Removechild,我已经考虑了一段时间了。每次尝试从ArrayList中删除项时,都会出现异常错误。与ArrayList相关的items.add工作得非常好,尽管我无法理解为什么它在删除items.remove时抛出异常 此ArrayList用于绘制和更新每个项目的库存。我使用典型的for循环遍历每个对象: 对于int i=0;i

我已经考虑了一段时间了。每次尝试从ArrayList中删除项时,都会出现异常错误。与ArrayList相关的items.add工作得非常好,尽管我无法理解为什么它在删除items.remove时抛出异常

此ArrayList用于绘制和更新每个项目的库存。我使用典型的for循环遍历每个对象:

对于int i=0;i 这是我的库存类的内部:

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());
}
}

关于如何克服这个问题有什么想法吗

编辑:这是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) 
我尝试将itemSize编辑为items.size,但不幸的是仍然发生错误。我认为itemCount不是问题所在,因为items.size没有超过值“8”

编辑:好的,我解决了问题。items.geti导致异常,原因如下:

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);
                }
            }

这是因为一旦库存打开,它就会尝试设置items.geti.bSelected=true或items.geti.bSelected=false。也许它想找到ArrayList中不存在的东西?仍然有相同的错误:/

编辑:实际上,您的代码不一定像我最初认为的那样工作。请提供其他人提到的堆栈跟踪,以便更容易确定问题所在

试着换个颜色

if (itemCount > 8) {
    items.remove(8);
}
将来

if (items.size() > 8) {
    items.remove(8);
}

您可能正在删除第二个类中未反映在itemCount中的项,因为您在第一个draw方法的顶部获得了该计数。

查看堆栈:触发异常的不是remove方法,而是get方法。看起来当你删除一个对象时,你的tileCount没有得到正确更新,是吗?

[修复]

我有

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

当要删除的项目甚至不存在时!我刚刚删除了这句话,效果非常好

您会遇到什么异常?如果您需要帮助,请将完整的stacktrace添加到您的问题中。如果是这样,我将如何确保它得到正确更新?有什么想法吗?
if (items.size() > 8) {
    items.remove(8);
}
if (items.get(i) != null) {
                    items.get(tileCount).bSelected = false;
                }