TheAlgorithms/Java · error · IllegalArgumentException

Input matrices must not be empty

Error message

Input matrices must not be empty

What it means

Thrown by MatrixMultiplication.multiply when either matrix has zero rows, or the first row of either matrix has zero columns (matrixA[0].length == 0 || matrixB[0].length == 0). An empty matrix has no defined product, so the guard fails fast instead of producing a zero-dimension result array. Note: this check accesses matrix[0] and will itself NPE if a row is null but the outer array is non-empty.

Source

Thrown at src/main/java/com/thealgorithms/matrix/MatrixMultiplication.java:44

    }

    /**
     * Multiplies two matrices.
     *
     * @param matrixA the first matrix rowsA x colsA
     * @param matrixB the second matrix rowsB x colsB
     * @return the product of the two matrices rowsA x colsB
     * @throws IllegalArgumentException if the matrices cannot be multiplied
     */
    public static double[][] multiply(double[][] matrixA, double[][] matrixB) {
        // Check the input matrices are not null
        if (matrixA == null || matrixB == null) {
            throw new IllegalArgumentException("Input matrices cannot be null");
        }

        // Check for empty matrices
        if (matrixA.length == 0 || matrixB.length == 0 || matrixA[0].length == 0 || matrixB[0].length == 0) {
            throw new IllegalArgumentException("Input matrices must not be empty");
        }

        // Validate the matrix dimensions
        if (matrixA[0].length != matrixB.length) {
            throw new IllegalArgumentException("Matrices cannot be multiplied: incompatible dimensions.");
        }

        int rowsA = matrixA.length;
        int colsA = matrixA[0].length;
        int colsB = matrixB[0].length;

        // Initialize the result matrix with zeros
        double[][] result = new double[rowsA][colsB];

        // Perform matrix multiplication
        for (int i = 0; i < rowsA; i++) {
            for (int j = 0; j < colsB; j++) {
                for (int k = 0; k < colsA; k++) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check dimensions before calling and skip/handle the empty case explicitly.
  2. Represent 'no matrix' as null and catch error 474 instead, or use Optional<double[][]>.
  3. Ensure data loaders throw on empty input rather than returning an empty matrix silently.

Example fix

// before
double[][] r = MatrixMultiplication.multiply(a, b);

// after
if (a.length == 0 || b.length == 0 || a[0].length == 0 || b[0].length == 0) {
    return new double[0][0]; // or throw a domain-specific exception
}
double[][] r = MatrixMultiplication.multiply(a, b);
Defensive patterns

Strategy: validation

Validate before calling

if (matrixA.length == 0 || matrixB.length == 0 || matrixA[0].length == 0 || matrixB[0].length == 0) {
    throw new IllegalArgumentException("matrices must be non-empty");
}
double[][] r = MatrixMultiplication.multiply(matrixA, matrixB);

Prevention

When it happens

Trigger: Call multiply(new double[0][], b), multiply(a, new double[][]{{}}), or pass a matrix built from an empty list (new double[list.size()][] with list empty).

Common situations: Filtering/grouping that produced zero rows, CSV parsed into an empty 2D array for a header-only file, or a placeholder new double[0][0] used as a 'no data' sentinel.

Related errors


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