{"record":{"id":"0b44ffc912cdfa2f","repo":"TheAlgorithms/Java","slug":"invalid-column-indices","errorCode":null,"errorMessage":"Invalid column indices","messagePattern":"Invalid column indices","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/prefixsum/PrefixSum2D.java","lineNumber":59,"sourceCode":"    }\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":41,"sourceCodeEnd":65,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/prefixsum/PrefixSum2D.java#L41-L65","documentation":"Thrown by PrefixSum2D.sumRegion(row1, col1, row2, col2) when the column indices are invalid: col1 < 0, col2 >= number of columns, or col2 < col1. Checked only after row indices pass. Bounds are 0-based inclusive against the original matrix column count.","triggerScenarios":"Call sumRegion(0, -1, 2, 2), sumRegion(0, 0, 2, cols) (col2 past last column), or sumRegion(0, 3, 2, 1) (col2 < col1).","commonSituations":"Passing column count as right index (cols instead of cols-1); inverted width bounds; 1-based columns not converted.","solutions":["Use inclusive 0-based indices: col2 should be lastColIndex (cols-1).","Normalize corners so col2 >= col1 (swap if inverted).","Convert 1-based external coordinates to 0-based before calling."],"exampleFix":"// before\nlong s = ps2.sumRegion(r1, c1, r2, c2); // c2 may equal column count\n\n// after\nlong s = ps2.sumRegion(r1, c1, r2, Math.min(c2, cols - 1));","handlingStrategy":"validation","validationCode":"int cols = matrix[0].length;\nif (col1 < 0 || col2 >= cols || col2 < col1) {\n    throw new IndexOutOfBoundsException(\"invalid column range\");\n}\nlong s = ps2.sumRegion(row1, col1, row2, col2);","typeGuard":"static boolean validColRange(int c1, int c2Inclusive, int cols) {\n    return c1 >= 0 && c2Inclusive < cols && c2Inclusive >= c1;\n}","tryCatchPattern":"try {\n    long s = ps2.sumRegion(r1, c1, r2, c2);\n} catch (IndexOutOfBoundsException e) {\n    logger.warn(\"Bad column range [{}, {}]\", c1, c2);\n}","preventionTips":["Use last column index (cols-1), never column count, as the right bound.","Normalize inverted width corners 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"}