TheAlgorithms/Java · error · IllegalArgumentException

The input matrix cannot be null

Error message

The input matrix cannot be null

What it means

Thrown by QRDecomposition.validateInputMatrix when the matrix argument passed to the decomposition is the Java null reference. QR decomposition requires a real-valued matrix to factor into Q (orthogonal) and R (upper triangular), so a null matrix has no dimensions or rows to operate on and the algorithm cannot proceed. This is a precondition check that fires before any Householder/normalization work begins.

Source

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

        }
        return result;
    }

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

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure the double[][] passed to QRDecomposition is instantiated (e.g., new double[m][n]) before the call.
  2. Check the upstream data source (file loader, parser, RPC) that produced the matrix and fix its null-return path.
  3. Add an explicit null guard at the call site that supplies a sensible default or fails earlier with a clearer message.

Example fix

// before
double[][] m = loadMatrix(path); // may return null
QRDecomposition.decompose(m);

// after
double[][] m = loadMatrix(path);
if (m == null) {
    throw new IllegalStateException("No matrix loaded from " + path);
}
QRDecomposition.decompose(m);
Defensive patterns

Strategy: validation

Validate before calling

if (matrix == null) {
    throw new IllegalStateException("Matrix required for QR decomposition");
}
QRDecomposition.decompose(matrix);

Type guard

static boolean isUsableMatrix(double[][] m) {
    return m != null && m.length > 0;
}

Prevention

When it happens

Trigger: Calling QRDecomposition with a double[][] variable that was never assigned (e.g., a field left null, a failed lookup that returns null, or a deserialized object missing its matrix). Any public QRDecomposition entry point that delegates to validateInputMatrix will throw on a null argument.

Common situations: Reading a matrix from a file/DB that returned null on missing data, refactoring that removed the assignment site, or passing the result of a method that returns null on error without checking.

Related errors


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