{"record":{"id":"f1b3bac5d96d5647","repo":"TheAlgorithms/Java","slug":"the-input-matrix-cannot-be-null-f1b3ba","errorCode":null,"errorMessage":"The input matrix cannot be null","messagePattern":"The input matrix cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java","lineNumber":31,"sourceCode":"\n    private MatrixUtil() {\n    }\n\n    private static boolean isValid(final BigDecimal[][] matrix) {\n        return matrix != null && matrix.length > 0 && matrix[0].length > 0;\n    }\n\n    private static boolean hasEqualSizes(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {\n        return isValid(matrix1) && isValid(matrix2) && matrix1.length == matrix2.length && matrix1[0].length == matrix2[0].length;\n    }\n\n    private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {\n        return isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length;\n    }\n\n    public static void validateInputMatrix(double[][] matrix) {\n        if (matrix == null) {\n            throw new IllegalArgumentException(\"The input matrix cannot be null\");\n        }\n        if (matrix.length == 0) {\n            throw new IllegalArgumentException(\"The input matrix cannot be empty\");\n        }\n        if (!hasValidRows(matrix)) {\n            throw new IllegalArgumentException(\"The input matrix cannot have null or empty rows\");\n        }\n        if (isJaggedMatrix(matrix)) {\n            throw new IllegalArgumentException(\"The input matrix cannot be jagged\");\n        }\n    }\n\n    private static boolean hasValidRows(double[][] matrix) {\n        for (double[] row : matrix) {\n            if (row == null || row.length == 0) {\n                return false;\n            }\n        }","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java#L13-L49","documentation":"Thrown by MatrixUtil.validateInputMatrix (the double[][] overload) when the matrix argument is null. MatrixUtil also handles BigDecimal matrices via separate helpers; this validator guards the primitive double matrix path. A null matrix has no dimensions or rows for subsequent hasValidRows/isJaggedMatrix checks, so it is rejected first.","triggerScenarios":"Calling a MatrixUtil operation with a double[][] that is null — e.g., a result of a method returning null on error, a field never assigned, or a deserialized object missing its matrix field.","commonSituations":"Upstream data source returns null on missing input, refactoring dropped the assignment, or a null propagated through a chain of operations.","solutions":["Ensure the double[][] is instantiated before calling MatrixUtil.","Fix the upstream null-return path in the data source.","Add an early null guard at the call site with a context-specific message."],"exampleFix":"// before\ndouble[][] a = readMatrix(file);\nMatrixUtil.validateInputMatrix(a); // throws if readMatrix returned null\n\n// after\ndouble[][] a = readMatrix(file);\nif (a == null) throw new IllegalStateException(\"Matrix missing for \" + file);\nMatrixUtil.validateInputMatrix(a);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(matrix, \"matrix\");\nMatrixUtil.validateInputMatrix(matrix);","typeGuard":"static boolean isUsableMatrix(double[][] m) {\n    return m != null && m.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Make matrix loaders return empty arrays, never null.","Add Objects.requireNonNull at the trust boundary."],"tags":["matrix","null-check","precondition","linear-algebra"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}