程序无法识别ArrayList索引

时间:2013-06-07 07:04:47

标签: java arraylist indexing

下面我发布了一段我的代码,我遇到了一些麻烦。每次运行此程序时,我都会收到错误消息

  

线程中的异常“AWT-EventQueue-1”java.lang.IndexOutOfBoundsException:Index:0,Size:0

或者换句话说,在我尝试稍后更改其中的值之前,没有创建ArrayList。然而,真正奇怪的是,当我在第一个for循环条件中替换realmHeight和realmWidth时,我没有问题,程序按预期工作。谁能解释为什么会这样?

顺便说一句,realmHeight和realmWidth将String作为文件名的参数,从该文件中读取并吐出正确的最大宽度和最大高度(在printlns中)。

public class GuiMap extends Gui {

    private ArrayList<ArrayList<GuiTile>> map = new ArrayList<ArrayList<GuiTile>>();
    private TileSet tileSet = new TileSet();
    private int tileSize;
    private String realm;

    public GuiMap(String name, String Realm) {
        super(name, 0, 0, 950, 600);
        realm = Realm;

        int realmHeight = getHeight() * 3;
        int realmWidth = getWidth()*3;

        try {
            realmHeight = countHeight(realm);
            realmWidth = countWidth(realm);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }       
        System.out.println("realmHeight: " + realmHeight + " realmWidth: " + realmWidth);

        tileSize = tileSet.get("grasslands")[0].getWidth();

        //make the map arrayList fill the entire screen
        int nameNum = 0;
        for (int i = 0; i < realmHeight; i++) {
            ArrayList<GuiTile> temporary = new ArrayList<GuiTile>();
            for (int j = 0; j < realmWidth; j++) {
                temporary.add(new GuiTile("Tile" + nameNum, j*tileSize, i*tileSize, tileSize, tileSet.get("grasslands")));
                nameNum++;
            }
            map.add(temporary);
        }

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(realm));
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("Couldn't find file to generate map from");
        }

        String currentLine = null;

        try {
            int i = 0;

            while ((currentLine = br.readLine()) != null) {
                char[] line = currentLine.toCharArray();
                ArrayList<GuiTile> temp = new ArrayList<GuiTile>();
                for (int j = 0; j < currentLine.length(); j++) {

                    switch (line[j]) {

                        case '0': map.get(i).get(j).tileValue = 0; break;
                        case '1': map.get(i).get(j).tileValue = 1; break;
                        case '2': map.get(i).get(j).tileValue = 2; break;
                        case '3': map.get(i).get(j).tileValue = 3; break;

                        default: map.get(i).get(j).tileValue = 0; break;
                    }
                }
                map.add(temp);
                i++;
            }
        }
        catch (IOException e) {
            System.out.println("Couldn't read from file. Please check map file");
        }
    }
}
编辑:Stacktrace:

Exception in thread "AWT-EventQueue-1" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at gg.mc.OneQuest.gui.GuiMap.<init>(GuiMap.java:81)
    at gg.mc.OneQuest.Game.<init>(Game.java:27)
    at gg.mc.OneQuest.gui.GuiLogin.login(GuiLogin.java:76)
    at gg.mc.OneQuest.gui.GuiLogin.access$0(GuiLogin.java:73)
    at gg.mc.OneQuest.gui.GuiLogin$1.run(GuiLogin.java:36)
    at gg.mc.OneQuest.gui.GuiButton.onClicked(GuiButton.java:30)
    at gg.mc.OneQuest.gui.Gui$1.run(Gui.java:31)
    at gg.mc.OneQuest.engine.Mouse$1.mouseClicked(Mouse.java:26)
    at java.awt.Component.processMouseEvent(Component.java:6507)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

1 个答案:

答案 0 :(得分:0)

错误已解决:

事实证明我需要在我最初创建的for循环中添加+1。我不太确定为什么我的程序在索引0处说我无法做到这一点,尽管我认为它可能位于未声明的ArrayList的最后一行。所以,我修改了错误,方法是将添加了tile的for循环使用<=而不是<

感谢帮助人员。