TheAlgorithms/Java · error · IllegalArgumentException
The input matrix cannot have null or empty rows
Error message
The input matrix cannot have null or empty rows
What it means
Thrown by MatrixUtil.validateInputMatrix when hasValidRows returns false, i.e., at least one row is null or has length 0. Matrix arithmetic indexes each row to a fixed column count, so a null or zero-length row breaks iteration. The helper short-circuits on the first offending row.
Source
Thrown at src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java:37
}
private static boolean hasEqualSizes(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
return isValid(matrix1) && isValid(matrix2) && matrix1.length == matrix2.length && matrix1[0].length == matrix2[0].length;
}
private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
return isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length;
}
public static void validateInputMatrix(double[][] matrix) {
if (matrix == null) {
throw new IllegalArgumentException("The input matrix cannot be null");
}
if (matrix.length == 0) {
throw new IllegalArgumentException("The input matrix cannot be empty");
}
if (!hasValidRows(matrix)) {
throw new IllegalArgumentException("The input matrix cannot have null or empty rows");
}
if (isJaggedMatrix(matrix)) {
throw new IllegalArgumentException("The input matrix cannot be jagged");
}
}
private static boolean hasValidRows(double[][] matrix) {
for (double[] row : matrix) {
if (row == null || row.length == 0) {
return false;
}
}
return true;
}
/**
* @brief Checks if the input matrix is a jagged matrix.
* Jagged matrix is a matrix where the number of columns in each row is not the same.View on GitHub (pinned to fdfb9a395b)
Solutions
- Allocate every inner array before the call.
- Reject or skip input lines that yield null/empty rows during parsing.
- Inspect matrix[i] for each i to locate the offending row.
Example fix
// before double[][] m = new double[2][]; // inner arrays null MatrixUtil.validateInputMatrix(m); // after double[][] m = new double[2][3]; MatrixUtil.validateInputMatrix(m);
Defensive patterns
Strategy: validation
Validate before calling
for (double[] row : matrix) {
if (row == null || row.length == 0) {
throw new IllegalStateException("Matrix contains a null/empty row");
}
} Type guard
static boolean allRowsValid(double[][] m) {
if (m == null) return false;
for (double[] row : m) if (row == null || row.length == 0) return false;
return true;
} Prevention
- Allocate 2D arrays with new double[rows][cols].
- Reject parsed lines that yield empty row arrays.
When it happens
Trigger: Passing new double[n][] with uninitialized inner arrays, a matrix with a row set to null after construction, or a parser that produced an empty row array for a blank input line.
Common situations: Partial initialization of a 2D array, inconsistent parsed lines, or a transform that left a trailing null row.
Related errors
- The input matrix cannot have null or empty rows
- The input matrix cannot be null
- The input matrix cannot be empty
- The input matrix cannot be jagged
- The input matrix cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/bea32e725a021173.
Report an issue: GitHub.