TheAlgorithms/Java · error · IllegalArgumentException

Maze must not be null or empty.

Error message

Maze must not be null or empty.

What it means

Thrown by RatInAMaze.findPaths(maze) when the maze array is null or has zero rows. The solver iterates over the matrix and indexes maze[0][0], so a null or empty array would cause an immediate failure. This guard rejects the degenerate input before any pathfinding begins.

Source

Thrown at src/main/java/com/thealgorithms/backtracking/RatInAMaze.java:46

 * @see <a href="https://en.wikipedia.org/wiki/Maze_solving_algorithm">Maze solving algorithm</a>
 * @author the-Sunny-Sharma (<a href="https://github.com/the-Sunny-Sharma">GitHub</a>)
 */
public final class RatInAMaze {

    private RatInAMaze() {
    }

    /**
     * Finds all paths from the top-left to the bottom-right of the given maze.
     *
     * @param maze an {@code n x n} binary matrix where {@code 1} = open, {@code 0} = blocked
     * @return a sorted list of all valid path strings using directions D, L, R, U;
     *         an empty list if no path exists
     * @throws IllegalArgumentException if the maze is null, empty, or not square
     */
    public static List<String> findPaths(final int[][] maze) {
        if (maze == null || maze.length == 0) {
            throw new IllegalArgumentException("Maze must not be null or empty.");
        }
        int n = maze.length;
        for (int[] row : maze) {
            if (row.length != n) {
                throw new IllegalArgumentException("Maze must be a square (n x n) matrix.");
            }
        }
        List<String> results = new ArrayList<>();
        if (maze[0][0] == 0 || maze[n - 1][n - 1] == 0) {
            return results;
        }
        boolean[][] visited = new boolean[n][n];
        solve(maze, 0, 0, n, "", visited, results);
        return results;
    }

    /**
     * Recursive backtracking helper that explores all four directions.

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure the maze is non-null and has at least one row before calling.
  2. Validate the input source (file/parser) and surface a clearer error when the matrix is empty.
  3. Default to an empty result instead of calling findPaths on missing data.

Example fix

// before
List<String> paths = RatInAMaze.findPaths(maze);  // throws if maze null/empty

// after
if (maze == null || maze.length == 0) return Collections.emptyList();
List<String> paths = RatInAMaze.findPaths(maze);
Defensive patterns

Strategy: type-guard

Validate before calling

public static boolean isNonEmptyMatrix(int[][] m) {
    return m != null && m.length > 0;
}
// usage
if (!isNonEmptyMatrix(maze)) return Collections.emptyList();
RatInAMaze.findPaths(maze);

Type guard

public static boolean isNonEmptyMatrix(int[][] m) {
    return m != null && m.length > 0;
}

Try / catch

try {
    return RatInAMaze.findPaths(maze);
} catch (IllegalArgumentException e) {
    return Collections.emptyList();
}

Prevention

When it happens

Trigger: Calling `findPaths(null)` or `findPaths(new int[0][])`. The check `maze == null || maze.length == 0` triggers; a 1x1 or larger non-square matrix passes this check but may fail the later square check.

Common situations: Maze loaded from a file that was empty or malformed; maze built from a list that turned out empty; deserialized matrix that came back null.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/2350e5172524963c. Report an issue: GitHub.