{"record":{"id":"18485182a717e7d3","repo":"TheAlgorithms/Java","slug":"matrix-must-not-be-null-or-empty","errorCode":null,"errorMessage":"Matrix must not be null or empty","messagePattern":"Matrix must not be null or empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/StochasticMatrix.java","lineNumber":71,"sourceCode":"\n        for (int j = 0; j < cols; j++) {\n            double sum = 0.0;\n            for (int i = 0; i < rows; i++) {\n                if (matrix[i][j] < 0) {\n                    return false;\n                }\n                sum += matrix[i][j];\n            }\n            if (Math.abs(sum - 1.0) > TOLERANCE) {\n                return false;\n            }\n        }\n        return true;\n    }\n\n    private static void validateMatrix(double[][] matrix) {\n        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {\n            throw new IllegalArgumentException(\"Matrix must not be null or empty\");\n        }\n    }\n}\n","sourceCodeStart":53,"sourceCodeEnd":75,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java#L53-L75","documentation":"Thrown by StochasticMatrix.validateMatrix when the matrix is null, has zero rows (matrix.length == 0), or its first row has zero length (matrix[0].length == 0). A stochastic (Markov transition) matrix must have rows of probabilities to verify, so an empty/null matrix has nothing to validate. The check only inspects matrix[0], so a non-empty first row passes even if later rows differ.","triggerScenarios":"Passing a null transition matrix, a newly-allocated double[0][], or a matrix whose first row is an empty double[0] array. Common when a transition table loaded from input produced no columns.","commonSituations":"Empty Markov model definition, a parser that produced no transition data, or a default-constructed matrix before probabilities are filled.","solutions":["Confirm the transition matrix source yields at least one row and one column.","Build the matrix with explicit dimensions before populating probabilities.","Guard the call: skip validation if the model genuinely has no states."],"exampleFix":"// before\ndouble[][] P = loadTransitions(state);\nStochasticMatrix.isStochastic(P); // throws if empty\n\n// after\ndouble[][] P = loadTransitions(state);\nif (P == null || P.length == 0 || P[0].length == 0) {\n    throw new IllegalStateException(\"No transitions defined for state \" + state);\n}\nStochasticMatrix.isStochastic(P);","handlingStrategy":"validation","validationCode":"if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {\n    throw new IllegalStateException(\"Transition matrix is empty\");\n}\nStochasticMatrix.isStochastic(matrix);","typeGuard":"static boolean isUsableTransitionMatrix(double[][] m) {\n    return m != null && m.length > 0 && m[0].length > 0;\n}","tryCatchPattern":null,"preventionTips":["Build the transition matrix with explicit dimensions before populating.","Validate the model loader yields a non-empty matrix."],"tags":["matrix","null-check","precondition","stochastic","markov"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}