TheAlgorithms/Java · error · IndexOutOfBoundsException

Invalid row indices

Error message

Invalid row indices

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/prefixsum/PrefixSum2D.java:56

                this.prefixSums[i + 1][j + 1] = matrix[i][j] + this.prefixSums[i][j + 1] + this.prefixSums[i + 1][j] - this.prefixSums[i][j];
            }
        }
    }

    /**
     * Calculates the sum of the sub-matrix defined by (row1, col1) to (row2, col2).
     * Indices are 0-based.
     *
     * @param row1 Top row index.
     * @param col1 Left column index.
     * @param row2 Bottom row index.
     * @param col2 Right column index.
     * @return The sum of the sub-matrix.
     * @throws IndexOutOfBoundsException if indices are invalid.
     */
    public long sumRegion(int row1, int col1, int row2, int col2) {
        if (row1 < 0 || row2 >= prefixSums.length - 1 || row2 < row1) {
            throw new IndexOutOfBoundsException("Invalid row indices");
        }
        if (col1 < 0 || col2 >= prefixSums[0].length - 1 || col2 < col1) {
            throw new IndexOutOfBoundsException("Invalid column indices");
        }

        return prefixSums[row2 + 1][col2 + 1] - prefixSums[row1][col2 + 1] - prefixSums[row2 + 1][col1] + prefixSums[row1][col1];
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Use inclusive 0-based indices: row2 should be lastRowIndex (rows-1).
  2. Normalize corners so row2 >= row1 before calling (swap if inverted).
  3. Convert 1-based external coordinates to 0-based at the boundary.

Example fix

// before
long s = ps2.sumRegion(r1, c1, r2, c2); // r2 may equal row count

// after
long s = ps2.sumRegion(r1, c1, Math.min(r2, rows - 1), Math.min(c2, cols - 1));
Defensive patterns

Strategy: validation

Validate before calling

int rows = matrix.length;
if (row1 < 0 || row2 >= rows || row2 < row1) {
    throw new IndexOutOfBoundsException("invalid row range");
}
long s = ps2.sumRegion(row1, col1, row2, col2);

Type guard

static boolean validRowRange(int r1, int r2Inclusive, int rows) {
    return r1 >= 0 && r2Inclusive < rows && r2Inclusive >= r1;
}

Try / catch

try {
    long s = ps2.sumRegion(r1, c1, r2, c2);
} catch (IndexOutOfBoundsException e) {
    logger.warn("Bad row range [{}, {}]", r1, r2);
}

Prevention

When it happens

Trigger: Call sumRegion(-1, 0, 2, 2), sumRegion(0, 0, rows, cols) (row2 past last row index), or sumRegion(3, 0, 1, 2) (row2 < row1).

Common situations: Passing row count as the bottom index (rows instead of rows-1); 1-based coordinates not converted; inverted rectangle corners (row2 above row1).

Related errors


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