ArrayIndexOutOfBoundsException for unknown reason

Greg Smith

I'm writing a program that creates a maze as a 2d array. I've run into a hiccup, and that is ArrayIndexOutOfBoundsException. It is pointing to maze[0][0] = "S" in the drawMaze method. I'm scratching my head at this, I have no idea why it's throwing an error.

import java.util.Random;

public class LA2_MazeSolver {

private int rows;
private int cols;
private String[][] maze = new String[rows][cols];

LA2_MazeInput mi = new LA2_MazeInput();

public void setNumRows(int numRows) {

    this.rows = numRows;

}

public void setNumCols(int numCols) {

    this.cols = numCols;

}

public int getNumRows() {

    return this.rows;

}

public int getNumCols() {

    return this.cols;

}

public void drawMaze() {

    Random r = new Random();

    maze[0][0] = "S";
    maze[rows - 1][cols - 1] = "D";
    int limit = ((rows * cols) / 3);

    for (int i = r.nextInt(limit) + 1; i < limit; i++) {

        maze[r.nextInt(rows) - 1][r.nextInt(cols) - 1] = "#";

    }

    for (int i = 0; i < maze.length; i++) {
        for (int c = 0; c < maze[0].length; c++) {

            if (!(maze[i][c].matches("#")) && !(maze[i][c].matches("S")) && !(maze[i][c].matches("D"))) {

                maze[i][c] = Integer.toString(r.nextInt(100) + 1);

            }

        }
    }

}

public void printMaze() {

}

/*public boolean isSolvable() {

    return solveMazeRecursively(this.rows, this.cols);

}

private boolean solveMazeRecursively(int row, int col) {

}*/

public void printResult() {

}
}
John Dengis

In Java, you can't make a definition like private String[][] maze = new String[rows][cols]; outside of the constructor. At that point in the the code, the value for row and col have not been defined and so something like private String[][] maze = new String[rows][cols]; won't have well defined behaviour (row and col may in fact be 0, who knows).

Try instead something like this:

    private int row;
    private int col;
    private String[][] maze;

    public LA2_MazeSolver(int row, int col) {
        this.row = row;
        this.col = col;
        maze = new String[row][col]
    }

Now when you create a LA2_MazerSolver object, you will dynamically create an array of the correct size to use (for example solver = new LA2_MazeSolver(row, col)).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the reason for this ArrayIndexOutOfBoundsException in Java?

From Dev

NaN for an unknown reason

From Dev

Oracle dead with unknown reason

From Dev

Getting a TypeError for unknown reason

From Dev

StringIndexOutOfBoundException for unknown reason

From Dev

MongoDB stops unknown reason

From Dev

Unknown reason for "Expected ':'" error in javascript

From Dev

Simple WYSIWYG not working, reason unknown

From Dev

WebSocket in UWP crashes with unknown reason

From Dev

Foreign constraint fails for unknown reason

From Dev

Android app crashing - unknown reason

From Dev

Segmentation fault for a weird unknown reason

From Dev

Javascript is not not working due to unknown reason

From Dev

Objects field changes for unknown reason

From Dev

Variable changing value for unknown reason?

From Dev

Vertical scrollbar appears for unknown reason

From Dev

Ubuntu freezing irregularly for unknown reason

From Dev

Android App crashing for unknown reason

From Dev

var function calling for unknown reason

From Dev

Java compiler calls java.lang.ArrayIndexOutOfBoundsException without any reason

From Java

How to debug websocket connection error with "Unknown reason"

From Dev

UITableView and UIRefreshControl being moved down for unknown reason

From Dev

some highcharts categories missing for unknown reason

From Dev

Spark SQL: TwitterUtils Streaming fails for unknown reason

From Dev

What is the reason define unknown enum in separate class?

From Dev

List comprehension is returning "NoneType" TypeError for unknown reason

From Dev

Windows 10 high memory usage (unknown reason)

From Dev

some highcharts categories missing for unknown reason

From Dev

unknown reason struts project is not working correctly?

Related Related

  1. 1

    What is the reason for this ArrayIndexOutOfBoundsException in Java?

  2. 2

    NaN for an unknown reason

  3. 3

    Oracle dead with unknown reason

  4. 4

    Getting a TypeError for unknown reason

  5. 5

    StringIndexOutOfBoundException for unknown reason

  6. 6

    MongoDB stops unknown reason

  7. 7

    Unknown reason for "Expected ':'" error in javascript

  8. 8

    Simple WYSIWYG not working, reason unknown

  9. 9

    WebSocket in UWP crashes with unknown reason

  10. 10

    Foreign constraint fails for unknown reason

  11. 11

    Android app crashing - unknown reason

  12. 12

    Segmentation fault for a weird unknown reason

  13. 13

    Javascript is not not working due to unknown reason

  14. 14

    Objects field changes for unknown reason

  15. 15

    Variable changing value for unknown reason?

  16. 16

    Vertical scrollbar appears for unknown reason

  17. 17

    Ubuntu freezing irregularly for unknown reason

  18. 18

    Android App crashing for unknown reason

  19. 19

    var function calling for unknown reason

  20. 20

    Java compiler calls java.lang.ArrayIndexOutOfBoundsException without any reason

  21. 21

    How to debug websocket connection error with "Unknown reason"

  22. 22

    UITableView and UIRefreshControl being moved down for unknown reason

  23. 23

    some highcharts categories missing for unknown reason

  24. 24

    Spark SQL: TwitterUtils Streaming fails for unknown reason

  25. 25

    What is the reason define unknown enum in separate class?

  26. 26

    List comprehension is returning "NoneType" TypeError for unknown reason

  27. 27

    Windows 10 high memory usage (unknown reason)

  28. 28

    some highcharts categories missing for unknown reason

  29. 29

    unknown reason struts project is not working correctly?

HotTag

Archive