-1

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());
    }
  • Which one is line 281? – Federico klez Culloca May 05 '22 at 14:57
  • 1
    And hint: in order to help you, we would need a [mcve]. As that whole Board code starts with reading data from a file. The exception could be due to "invalid" content in the file you are reading in. But we don't know that. Given that the exception TELLS you that your array HAS LENGTH 0 ... I guess that maybe height/width are read as 0 ... so your code creates arrays of length 0, and of course, when you try to access ANY element in a 0-length array, you get an error. – GhostCat May 05 '22 at 15:07
  • 1
    Seriously, the answer here is: step back. It is your assignment, your project. YOU have to spend the time to understand what each line in that code is doing. Turning to strangers on the internet for explanations is like the "least preferred option". Start with your peers or instructors. Talk to real people. Sit down, work with that code for HOURS. There are no detours in learning programming. – GhostCat May 05 '22 at 15:08
  • @GhostCat Thanks alot for the comment... Yh I think i have to sit down and understand the code well – Bernard Mensah May 05 '22 at 15:18

0 Answers0