{"record":{"id":"2350e5172524963c","repo":"TheAlgorithms/Java","slug":"maze-must-not-be-null-or-empty","errorCode":null,"errorMessage":"Maze must not be null or empty.","messagePattern":"Maze must not be null or empty\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/backtracking/RatInAMaze.java","lineNumber":46,"sourceCode":" * @see <a href=\"https://en.wikipedia.org/wiki/Maze_solving_algorithm\">Maze solving algorithm</a>\n * @author the-Sunny-Sharma (<a href=\"https://github.com/the-Sunny-Sharma\">GitHub</a>)\n */\npublic final class RatInAMaze {\n\n    private RatInAMaze() {\n    }\n\n    /**\n     * Finds all paths from the top-left to the bottom-right of the given maze.\n     *\n     * @param maze an {@code n x n} binary matrix where {@code 1} = open, {@code 0} = blocked\n     * @return a sorted list of all valid path strings using directions D, L, R, U;\n     *         an empty list if no path exists\n     * @throws IllegalArgumentException if the maze is null, empty, or not square\n     */\n    public static List<String> findPaths(final int[][] maze) {\n        if (maze == null || maze.length == 0) {\n            throw new IllegalArgumentException(\"Maze must not be null or empty.\");\n        }\n        int n = maze.length;\n        for (int[] row : maze) {\n            if (row.length != n) {\n                throw new IllegalArgumentException(\"Maze must be a square (n x n) matrix.\");\n            }\n        }\n        List<String> results = new ArrayList<>();\n        if (maze[0][0] == 0 || maze[n - 1][n - 1] == 0) {\n            return results;\n        }\n        boolean[][] visited = new boolean[n][n];\n        solve(maze, 0, 0, n, \"\", visited, results);\n        return results;\n    }\n\n    /**\n     * Recursive backtracking helper that explores all four directions.","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/backtracking/RatInAMaze.java#L28-L64","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the maze is non-null and has at least one row before calling.","Validate the input source (file/parser) and surface a clearer error when the matrix is empty.","Default to an empty result instead of calling findPaths on missing data."],"exampleFix":"// before\nList<String> paths = RatInAMaze.findPaths(maze);  // throws if maze null/empty\n\n// after\nif (maze == null || maze.length == 0) return Collections.emptyList();\nList<String> paths = RatInAMaze.findPaths(maze);","handlingStrategy":"type-guard","validationCode":"public static boolean isNonEmptyMatrix(int[][] m) {\n    return m != null && m.length > 0;\n}\n// usage\nif (!isNonEmptyMatrix(maze)) return Collections.emptyList();\nRatInAMaze.findPaths(maze);","typeGuard":"public static boolean isNonEmptyMatrix(int[][] m) {\n    return m != null && m.length > 0;\n}","tryCatchPattern":"try {\n    return RatInAMaze.findPaths(maze);\n} catch (IllegalArgumentException e) {\n    return Collections.emptyList();\n}","preventionTips":["Null-check matrices loaded from files/parsers.","Return an empty result instead of calling on missing data.","Validate row count > 0 at the loader boundary."],"tags":["backtracking","matrix","null-check","argument-validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}