I am trying to understand a project someone gave me as a reference to work on a project. The code gives me the following error when I run the code: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at Board.main(Board.java:281)
Code below:
Constructor:
public Board(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
String[] dims = line.split("\\s+", 2);
width = Integer.parseInt(dims[0], 10);
height = Integer.parseInt(dims[1], 10);
board = new byte[width][height];
targets = new HashSet<Point>();
char[][] input = new char[2 * width - 1][2 * height - 1];
for (int y = 0; y < 2 * height - 1; y++) {
line = br.readLine();
for (int x = 0; x < 2 * width - 1 && x < line.length(); x++) {
input[x][y] = line.charAt(x);
}
}
br.close();
br = null;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
byte tile = 0;
// can go left?
if (x > 0 && input[2 * x - 1][2 * y] != '|') {
tile |= BIT_LEFT;
}
if (x < width - 1 && input[2 * x + 1][2 * y] != '|') {
tile |= BIT_RIGHT;
}
if (y > 0 && input[2 * x][2 * y - 1] != '-') {
tile |= BIT_UP;
}
if (y < height - 1 && input[2 * x][2 * y + 1] != '-') {
tile |= BIT_DOWN;
}
board[x][y] = tile;
if (input[2 * x][2 * y] == 'X') {
targets.add(new Point(x, y));
}
}
}
}
And here is the main method:
public static void main(String[] args) throws IOException {
Board board = new Board(new FileInputStream(new File(args[0])));
System.out.println(board.toString());
}