0

下面我发布了一段我遇到问题的代码片段。每次我运行这个程序时,我都会收到一条错误消息

线程“AWT-EventQueue-1”中的异常 java.lang.IndexOutOfBoundsException:索引:0,大小:0

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

顺便说一句,realmHeight 和 realmWidth 将字符串作为文件名的参数,从该文件中读取并输出正确的最大宽度和最大高度(在 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");
        }
    }
}

编辑:堆栈跟踪:

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)
4

1 回答 1

0

错误已解决:

事实证明,我需要+1在我最初创建的 for 循环中添加一个。我不太确定为什么我的程序说它在索引 0 处我无法做到,尽管我想它可能在未声明的 ArrayList 的最后一行。因此,我通过使添加图块的 for 循环使用<=而不是<.

谢谢你们的帮助。

于 2013-06-07T12:14:11.263 回答