java - Array Out of Bounce Exception for Jar, but not Eclipse -
when convert java
project jar file, arrayindexoutofbounds
exception of -2 occurs in line 3 of section of code:
for (int = 0; < copy.get(copy.size() - 2).size(); i++) { if (!copy.get(copy.size() - 2).get(i).tostring().equals(" ")) { startlocations[index] = integer.parseint(copy.get(copy.size() - 2).get(i).tostring()); index++; } }
i find strange because program runs fine in eclipse, , size of copy
, 2d arraylist
tempoarily holding grid of maze 9. code fails when convert jar , run cmd. here full relevant code below. files in correct locations.
string filename = "maze.txt"; string line = null; arraylist < arraylist < square >> grid = new arraylist < arraylist < square >> (); try { // setup filereader, bufferedreader, , linereader filereader filereader = new filereader(filename); bufferedreader bufferedreader = new bufferedreader(filereader); int row = 0; // lines inside maze file while ((line = bufferedreader.readline()) != null) { arraylist < square > linelist = new arraylist < square > (); (int = 0; < line.length(); i++) { string letter = character.tostring(line.charat(i)); if (letter.equals("l")) { linelist.add(new lockedsquare(row, i, letter)); } else { linelist.add(new square(row, i, letter)); } row++; } grid.add(linelist); } // cut down grid maze arraylist < arraylist < square >> copy = grid; int length = grid.size(); grid = new arraylist < arraylist < square >> (); (int = 0; < length - 2; i++) { grid.add(copy.get(i)); } // start position int[] startlocations = new int[2]; int index = 0; (int = 0; < copy.get(copy.size() - 2).size(); i++) { if (!copy.get(copy.size() - 2).get(i).tostring().equals(" ")) { startlocations[index] = integer.parseint(copy.get(copy.size() - 2).get(i).tostring()); index++; } } int playerx = startlocations[0]; int playery = startlocations[1]; } // exceptions catch (filenotfoundexception e) { system.out.println("error: maze.txt not found"); } catch (ioexception ex) { system.out.println("error reading maze.txt"); }
this happening because jar file unable find maze.txt
.
change string filename = "maze.txt";
string filename = <absolute-path-for-maze.txt> ;
, should through in jar
Comments
Post a Comment