TheAlgorithms/Java · error · IllegalArgumentException
Input matrix cannot be null or empty
Error message
Input matrix cannot be null or empty
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/prefixsum/PrefixSum2D.java:28
* <p>This implementation uses a long array for the prefix sums to prevent
* integer overflow.
*
* @see <a href="https://en.wikipedia.org/wiki/Summed-area_table">Summed-area table (Wikipedia)</a>
* @author Chahat Sandhu, <a href="https://github.com/singhc7">singhc7</a>
*/
public class PrefixSum2D {
private final long[][] prefixSums;
/**
* Constructor to preprocess the input matrix.
*
* @param matrix The input integer matrix.
* @throws IllegalArgumentException if the matrix is null or empty.
*/
public PrefixSum2D(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
throw new IllegalArgumentException("Input matrix cannot be null or empty");
}
int rows = matrix.length;
int cols = matrix[0].length;
this.prefixSums = new long[rows + 1][cols + 1];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// P[i+1][j+1] = current + above + left - diagonal_overlap
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.
*View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
PrefixSum2D ps2 = new PrefixSum2D(grid); // grid may be empty
// after
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return; // or throw domain-specific exception
}
PrefixSum2D ps2 = new PrefixSum2D(grid); Defensive patterns
Strategy: validation
Validate before calling
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
throw new IllegalArgumentException("matrix must be non-null and non-empty in both dims");
}
PrefixSum2D ps2 = new PrefixSum2D(matrix); Type guard
static boolean usableMatrix(int[][] m) {
return m != null && m.length > 0 && m[0].length > 0;
} Try / catch
try {
PrefixSum2D ps2 = new PrefixSum2D(matrix);
} catch (IllegalArgumentException e) {
logger.warn("Empty/null matrix rejected by PrefixSum2D");
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Input array cannot be null
- Input array cannot be null
- Prefix sum array cannot be null
- Input array cannot be null
- Capacity matrix must not be null or empty
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/d28fa5425c8219d5.
Report an issue: GitHub.