{"record":{"id":"18fa0d6ef5afd116","repo":"TheAlgorithms/Java","slug":"invalid-row-indices","errorCode":null,"errorMessage":"Invalid row indices","messagePattern":"Invalid row indices","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/prefixsum/PrefixSum2D.java","lineNumber":56,"sourceCode":"                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     *\n     * @param row1 Top row index.\n     * @param col1 Left column index.\n     * @param row2 Bottom row index.\n     * @param col2 Right column index.\n     * @return The sum of the sub-matrix.\n     * @throws IndexOutOfBoundsException if indices are invalid.\n     */\n    public long sumRegion(int row1, int col1, int row2, int col2) {\n        if (row1 < 0 || row2 >= prefixSums.length - 1 || row2 < row1) {\n            throw new IndexOutOfBoundsException(\"Invalid row indices\");\n        }\n        if (col1 < 0 || col2 >= prefixSums[0].length - 1 || col2 < col1) {\n            throw new IndexOutOfBoundsException(\"Invalid column indices\");\n        }\n\n        return prefixSums[row2 + 1][col2 + 1] - prefixSums[row1][col2 + 1] - prefixSums[row2 + 1][col1] + prefixSums[row1][col1];\n    }\n}\n","sourceCodeStart":38,"sourceCodeEnd":65,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/prefixsum/PrefixSum2D.java#L38-L65","documentation":"Thrown by PrefixSum2D.sumRegion(row1, col1, row2, col2) when the row indices are invalid: row1 < 0, row2 >= number of rows, or row2 < row1. Bounds are 0-based inclusive. Checked before column indices, so a bad row fails first even if columns are also bad.","triggerScenarios":"Call sumRegion(-1, 0, 2, 2), sumRegion(0, 0, rows, cols) (row2 past last row index), or sumRegion(3, 0, 1, 2) (row2 < row1).","commonSituations":"Passing row count as the bottom index (rows instead of rows-1); 1-based coordinates not converted; inverted rectangle corners (row2 above row1).","solutions":["Use inclusive 0-based indices: row2 should be lastRowIndex (rows-1).","Normalize corners so row2 >= row1 before calling (swap if inverted).","Convert 1-based external coordinates to 0-based at the boundary."],"exampleFix":"// before\nlong s = ps2.sumRegion(r1, c1, r2, c2); // r2 may equal row count\n\n// after\nlong s = ps2.sumRegion(r1, c1, Math.min(r2, rows - 1), Math.min(c2, cols - 1));","handlingStrategy":"validation","validationCode":"int rows = matrix.length;\nif (row1 < 0 || row2 >= rows || row2 < row1) {\n    throw new IndexOutOfBoundsException(\"invalid row range\");\n}\nlong s = ps2.sumRegion(row1, col1, row2, col2);","typeGuard":"static boolean validRowRange(int r1, int r2Inclusive, int rows) {\n    return r1 >= 0 && r2Inclusive < rows && r2Inclusive >= r1;\n}","tryCatchPattern":"try {\n    long s = ps2.sumRegion(r1, c1, r2, c2);\n} catch (IndexOutOfBoundsException e) {\n    logger.warn(\"Bad row range [{}, {}]\", r1, r2);\n}","preventionTips":["Use last index (rows-1), never row count, as the bottom bound.","Normalize inverted corners (swap r1/r2) before calling.","Convert 1-based coordinates to 0-based at the boundary."],"tags":["prefix-sum","input-validation","index-out-of-bounds","matrix","off-by-one"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}