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 QRDecomposition.validateInputMatrix when hasValidRows returns false, meaning at least one row is null or has length 0. QR decomposition indexes every row to the same column count, so a null or zero-length row breaks dot-product and normalization loops. The check uses a short-circuit helper that returns false on the first offending row.

Source

Thrown at src/main/java/com/thealgorithms/matrix/QRDecomposition.java:124

        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;
    }

    private static boolean isJaggedMatrix(double[][] matrix) {
        int numColumns = matrix[0].length;
        for (double[] row : matrix) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Allocate inner arrays for every row before the call (new double[n][m]).
  2. Sanitize parsed input: skip or reject lines that yield null/empty row arrays.
  3. Inspect each matrix[i] in a debugger/log to find the offending index before the call.

Example fix

// before
double[][] m = new double[3][]; // inner arrays null
m[0] = new double[]{1,2};
// m[1] forgotten -> null
QRDecomposition.decompose(m);

// after
double[][] m = new double[3][2]; // all rows allocated
m[0][0]=1; m[0][1]=2; // ... fill remaining
QRDecomposition.decompose(m);
Defensive patterns

Strategy: validation

Validate before calling

for (double[] row : matrix) {
    if (row == null || row.length == 0) {
        throw new IllegalStateException("Matrix has 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

When it happens

Trigger: Passing new double[3][] (rows allocated but inner arrays null), a matrix where one row was set to null after construction, or a parsed matrix where one line of input was blank and produced an empty row array.

Common situations: Partial initialization (outer array sized but inner arrays not), mixed data lines with one empty record, or a copy/transform that left a trailing null row.

Related errors


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