TheAlgorithms/Java · error · IllegalArgumentException

Matrix cannot be null or empty

Error message

Matrix cannot be null or empty

What it means

Thrown by Sparsity.sparsity when the matrix is null, has zero rows, or its first row has zero length (matrix[0].length == 0). Sparsity is the fraction of zero elements; with no elements the ratio is undefined (division by totalElements=0). The check uses matrix[0], so only the first row's length is inspected.

Source

Thrown at src/main/java/com/thealgorithms/misc/Sparsity.java:27

 * sparsity = (number of zero elements) / (total number of elements)
 *
 * This can lead to significant computational optimizations.
 */
public final class Sparsity {

    private Sparsity() {
    }

    /**
     * Calculates the sparsity of a given 2D matrix.
     *
     * @param matrix the input matrix
     * @return the sparsity value between 0 and 1
     * @throws IllegalArgumentException if the matrix is null, empty, or contains empty rows
     */
    public static double sparsity(double[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            throw new IllegalArgumentException("Matrix cannot be null or empty");
        }

        int zeroCount = 0;
        int totalElements = 0;

        // Count the number of zero elements and total elements
        for (double[] row : matrix) {
            for (double value : row) {
                if (value == 0.0) {
                    zeroCount++;
                }
                totalElements++;
            }
        }

        // Return sparsity as a double
        return (double) zeroCount / totalElements;
    }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Confirm the matrix source produces at least one row and one column.
  2. Skip sparsity computation for empty matrices if empty is legitimate.
  3. Build the matrix with explicit dimensions before populating.

Example fix

// before
double[][] m = loadSparseMatrix(path);
double s = Sparsity.sparsity(m); // throws if empty

// after
double[][] m = loadSparseMatrix(path);
if (m == null || m.length == 0 || m[0].length == 0) {
    return 0.0; // or handle as a domain-specific sentinel
double s = Sparsity.sparsity(m);
Defensive patterns

Strategy: validation

Validate before calling

if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
    return 0.0; // or throw with domain context
}
return Sparsity.sparsity(matrix);

Type guard

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

Prevention

When it happens

Trigger: Passing null, new double[0][], or a matrix whose first row is double[0] (e.g., a sparse dataset with no columns). Common with empty datasets or a parser producing no columns.

Common situations: Empty input dataset, a CSV with a header but no data columns, or a default-constructed matrix before filling.

Related errors


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