Note that there are some explanatory texts on larger screens.

plurals
  1. POProgram not recognizing ArrayList Index
    primarykey
    data
    text
    <p>Below I've posted a snippet of my code that I am having some trouble with. Every time I run this program I get an error saying that </p> <blockquote> <p>Exception in thread "AWT-EventQueue-1" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0</p> </blockquote> <p>Or in other words that the ArrayList isn't being created before I try and change values in it later on. What's really weird, however, is that when I replace realmHeight and realmWidth in the first for loop condition then I have no issues and the program works as intended. Could anyone shed some light as to why this is?</p> <p>Btw, realmHeight and realmWidth take a String as a parameter for a filename, read from that file and spit out the correct max width and max height (in the printlns).</p> <pre><code>public class GuiMap extends Gui { private ArrayList&lt;ArrayList&lt;GuiTile&gt;&gt; map = new ArrayList&lt;ArrayList&lt;GuiTile&gt;&gt;(); 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 &lt; realmHeight; i++) { ArrayList&lt;GuiTile&gt; temporary = new ArrayList&lt;GuiTile&gt;(); for (int j = 0; j &lt; 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&lt;GuiTile&gt; temp = new ArrayList&lt;GuiTile&gt;(); for (int j = 0; j &lt; 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"); } } } </code></pre> <p>EDIT: Stacktrace:</p> <pre><code>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.&lt;init&gt;(GuiMap.java:81) at gg.mc.OneQuest.Game.&lt;init&gt;(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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload