TheAlgorithms/Java · error · IllegalArgumentException
The input matrix cannot be empty
Error message
The input matrix cannot be empty
What it means
Thrown by QRDecomposition.validateInputMatrix when matrix.length == 0, i.e., the outer array exists but contains zero rows. QR decomposition operates on row/column data, so an empty matrix (0x0) has no diagonal to iterate over and no vectors to normalize. The check runs after the null check and before row validation.
Source
Thrown at src/main/java/com/thealgorithms/matrix/QRDecomposition.java:121
private static double[] scalarMultiply(double[] v, double scalar) {
double[] result = new double[v.length];
for (int i = 0; i < v.length; i++) {
result[i] = v[i] * scalar;
}
return result;
}
private static double norm(double[] v) {
return Math.sqrt(dotProduct(v, v));
}
private 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 actually contains rows before building the double[][].
- If an empty matrix is a legitimate case in your domain, branch around the call instead of passing it through.
- Log the matrix dimensions immediately before the call to confirm row count.
Example fix
// before
List<double[]> rows = readRows(file);
double[][] m = rows.toArray(new double[0][]);
QRDecomposition.decompose(m);
// after
List<double[]> rows = readRows(file);
if (rows.isEmpty()) {
throw new IllegalStateException("Input file contained no matrix rows");
double[][] m = rows.toArray(new double[0][]);
QRDecomposition.decompose(m); Defensive patterns
Strategy: validation
Validate before calling
if (matrix == null || matrix.length == 0) {
throw new IllegalStateException("QR decomposition needs a non-empty matrix");
} Type guard
static boolean isNonEmptyMatrix(double[][] m) {
return m != null && m.length > 0;
} Prevention
- Treat an empty matrix as a domain decision, not an algorithm input: branch before calling.
- Log matrix dimensions right before decomposition to catch empty inputs early.
When it happens
Trigger: Passing new double[0][], a freshly allocated empty matrix from a constructor, or a filtered/collected matrix whose source produced no rows (e.g., an empty CSV file parsed into rows).
Common situations: Empty input dataset, an upstream filter that removed all rows, a test fixture that builds a matrix from an empty list via toArray(new double[0][]), or a default-initialized matrix before data is appended.
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/7e02c37507492051.
Report an issue: GitHub.