{"record":{"id":"d28fa5425c8219d5","repo":"TheAlgorithms/Java","slug":"input-matrix-cannot-be-null-or-empty","errorCode":null,"errorMessage":"Input matrix cannot be null or empty","messagePattern":"Input matrix cannot be null or empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/prefixsum/PrefixSum2D.java","lineNumber":28,"sourceCode":" * <p>This implementation uses a long array for the prefix sums to prevent\n * integer overflow.\n *\n * @see <a href=\"https://en.wikipedia.org/wiki/Summed-area_table\">Summed-area table (Wikipedia)</a>\n * @author Chahat Sandhu, <a href=\"https://github.com/singhc7\">singhc7</a>\n */\npublic class PrefixSum2D {\n\n    private final long[][] prefixSums;\n\n    /**\n     * Constructor to preprocess the input matrix.\n     *\n     * @param matrix The input integer matrix.\n     * @throws IllegalArgumentException if the matrix is null or empty.\n     */\n    public PrefixSum2D(int[][] matrix) {\n        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {\n            throw new IllegalArgumentException(\"Input matrix cannot be null or empty\");\n        }\n\n        int rows = matrix.length;\n        int cols = matrix[0].length;\n        this.prefixSums = new long[rows + 1][cols + 1];\n\n        for (int i = 0; i < rows; i++) {\n            for (int j = 0; j < cols; j++) {\n                // P[i+1][j+1] = current + above + left - diagonal_overlap\n                this.prefixSums[i + 1][j + 1] = matrix[i][j] + this.prefixSums[i][j + 1] + this.prefixSums[i + 1][j] - this.prefixSums[i][j];\n            }\n        }\n    }\n\n    /**\n     * Calculates the sum of the sub-matrix defined by (row1, col1) to (row2, col2).\n     * Indices are 0-based.\n     *","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/prefixsum/PrefixSum2D.java#L10-L46","documentation":"Thrown by the PrefixSum2D constructor when the matrix is null, has zero rows, or its first row has zero columns. The constructor immediately reads matrix[0].length, so all three states are invalid.","triggerScenarios":"new PrefixSum2D(null), new PrefixSum2D(new int[0][]), or new PrefixSum2D(new int[][]{{}}) (a row with zero columns). A jagged row with zero columns only on a later row is NOT caught here.","commonSituations":"A 2D array from a grid query that returned no cells; an image/matrix load producing zero rows; nested collections flattened with an empty inner list for row 0.","solutions":["Check matrix != null && matrix.length > 0 && matrix[0].length > 0 before constructing.","Validate every row has the same non-zero column count if your source can be jagged (the constructor only checks row 0).","Handle the empty-grid case at the caller rather than constructing."],"exampleFix":"// before\nPrefixSum2D ps2 = new PrefixSum2D(grid); // grid may be empty\n\n// after\nif (grid == null || grid.length == 0 || grid[0].length == 0) {\n    return; // or throw domain-specific exception\n}\nPrefixSum2D ps2 = new PrefixSum2D(grid);","handlingStrategy":"validation","validationCode":"if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {\n    throw new IllegalArgumentException(\"matrix must be non-null and non-empty in both dims\");\n}\nPrefixSum2D ps2 = new PrefixSum2D(matrix);","typeGuard":"static boolean usableMatrix(int[][] m) {\n    return m != null && m.length > 0 && m[0].length > 0;\n}","tryCatchPattern":"try {\n    PrefixSum2D ps2 = new PrefixSum2D(matrix);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Empty/null matrix rejected by PrefixSum2D\");\n}","preventionTips":["Validate row 0 non-empty, but also assert all rows share the same column count — the constructor does not.","Handle the empty-grid case at the caller rather than constructing.","Normalize jagged input to rectangular before passing."],"tags":["prefix-sum","input-validation","illegal-argument","matrix","null-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}