TheAlgorithms/Java · error · IllegalArgumentException
The input matrix cannot be empty
Error message
The input matrix cannot be empty
What it means
Thrown by MatrixUtil.validateInputMatrix when matrix.length == 0 — the outer array exists but contains zero rows. Matrix operations need at least one row to determine column count and perform arithmetic; an empty matrix (0x0) is degenerate. This check follows the null check and precedes the row-validity check.
Source
Thrown at src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java:34
private static boolean isValid(final BigDecimal[][] matrix) {
return matrix != null && matrix.length > 0 && matrix[0].length > 0;
}
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;
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Verify the data source yields at least one row before building the matrix.
- Branch around the operation if an empty matrix is legitimate in your domain.
- Log matrix.length right before the call to confirm it is zero.
Example fix
// before
double[][] m = rows.toArray(new double[0][]);
MatrixUtil.validateInputMatrix(m); // throws when rows empty
// after
if (rows.isEmpty()) throw new IllegalStateException("No rows to build matrix");
double[][] m = rows.toArray(new double[0][]);
MatrixUtil.validateInputMatrix(m); Defensive patterns
Strategy: validation
Validate before calling
if (matrix == null || matrix.length == 0) {
throw new IllegalStateException("Matrix must have at least one row");
} Type guard
static boolean isNonEmptyMatrix(double[][] m) {
return m != null && m.length > 0;
} Prevention
- Branch on empty matrices before calling matrix utilities.
- Log matrix.length to catch empty inputs early.
When it happens
Trigger: Passing new double[0][], an empty matrix from a list-to-array conversion with no elements, or a parser that produced no rows from blank input.
Common situations: Empty input file, an upstream filter that removed all rows, or a default-initialized matrix before appending data.
Related errors
- The input matrix cannot be empty
- The input matrix cannot be null
- The input matrix cannot have null or empty rows
- 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/dba9b595dca06909.
Report an issue: GitHub.