以下に、問題が発生しているコードのスニペットを投稿しました。このプログラムを実行するたびに、次のエラーが表示されます
スレッド「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)