{"record":{"id":"34dbfdbd79a70a4f","repo":"TheAlgorithms/Java","slug":"matrix-cannot-be-null-or-empty","errorCode":null,"errorMessage":"Matrix cannot be null or empty","messagePattern":"Matrix cannot be null or empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/misc/Sparsity.java","lineNumber":27,"sourceCode":" * sparsity = (number of zero elements) / (total number of elements)\n *\n * This can lead to significant computational optimizations.\n */\npublic final class Sparsity {\n\n    private Sparsity() {\n    }\n\n    /**\n     * Calculates the sparsity of a given 2D matrix.\n     *\n     * @param matrix the input matrix\n     * @return the sparsity value between 0 and 1\n     * @throws IllegalArgumentException if the matrix is null, empty, or contains empty rows\n     */\n    public static double sparsity(double[][] matrix) {\n        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {\n            throw new IllegalArgumentException(\"Matrix cannot be null or empty\");\n        }\n\n        int zeroCount = 0;\n        int totalElements = 0;\n\n        // Count the number of zero elements and total elements\n        for (double[] row : matrix) {\n            for (double value : row) {\n                if (value == 0.0) {\n                    zeroCount++;\n                }\n                totalElements++;\n            }\n        }\n\n        // Return sparsity as a double\n        return (double) zeroCount / totalElements;\n    }","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/misc/Sparsity.java#L9-L45","documentation":"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.","triggerScenarios":"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.","commonSituations":"Empty input dataset, a CSV with a header but no data columns, or a default-constructed matrix before filling.","solutions":["Confirm the matrix source produces at least one row and one column.","Skip sparsity computation for empty matrices if empty is legitimate.","Build the matrix with explicit dimensions before populating."],"exampleFix":"// before\ndouble[][] m = loadSparseMatrix(path);\ndouble s = Sparsity.sparsity(m); // throws if empty\n\n// after\ndouble[][] m = loadSparseMatrix(path);\nif (m == null || m.length == 0 || m[0].length == 0) {\n    return 0.0; // or handle as a domain-specific sentinel\ndouble s = Sparsity.sparsity(m);","handlingStrategy":"validation","validationCode":"if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {\n    return 0.0; // or throw with domain context\n}\nreturn Sparsity.sparsity(matrix);","typeGuard":"static boolean isComputableMatrix(double[][] m) {\n    return m != null && m.length > 0 && m[0].length > 0;\n}","tryCatchPattern":null,"preventionTips":["Treat an empty matrix as a domain case, not an input to the ratio.","Validate the data source yields rows and columns."],"tags":["matrix","null-check","precondition","sparsity","statistics"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}