{"record":{"id":"97754e3ef597b539","repo":"TheAlgorithms/Java","slug":"input-matrices-cannot-be-null","errorCode":null,"errorMessage":"Input matrices cannot be null","messagePattern":"Input matrices cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/MatrixMultiplication.java","lineNumber":39,"sourceCode":" *\n */\n\npublic final class MatrixMultiplication {\n    private MatrixMultiplication() {\n    }\n\n    /**\n     * Multiplies two matrices.\n     *\n     * @param matrixA the first matrix rowsA x colsA\n     * @param matrixB the second matrix rowsB x colsB\n     * @return the product of the two matrices rowsA x colsB\n     * @throws IllegalArgumentException if the matrices cannot be multiplied\n     */\n    public static double[][] multiply(double[][] matrixA, double[][] matrixB) {\n        // Check the input matrices are not null\n        if (matrixA == null || matrixB == null) {\n            throw new IllegalArgumentException(\"Input matrices cannot be null\");\n        }\n\n        // Check for empty matrices\n        if (matrixA.length == 0 || matrixB.length == 0 || matrixA[0].length == 0 || matrixB[0].length == 0) {\n            throw new IllegalArgumentException(\"Input matrices must not be empty\");\n        }\n\n        // Validate the matrix dimensions\n        if (matrixA[0].length != matrixB.length) {\n            throw new IllegalArgumentException(\"Matrices cannot be multiplied: incompatible dimensions.\");\n        }\n\n        int rowsA = matrixA.length;\n        int colsA = matrixA[0].length;\n        int colsB = matrixB[0].length;\n\n        // Initialize the result matrix with zeros\n        double[][] result = new double[rowsA][colsB];","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/MatrixMultiplication.java#L21-L57","documentation":"Thrown by MatrixMultiplication.multiply when matrixA or matrixB is null. The null check runs first, before any indexing, so a null operand produces a clear message rather than a NullPointerException on matrixA.length / matrixB.length.","triggerScenarios":"Call multiply(null, b), multiply(a, null), or pass a matrix reference that was never assigned (e.g., a field left null after a failed initialization).","commonSituations":"An optional matrix parameter that arrived empty, a deserialization that produced null for an absent JSON field, or a code path that constructs the second operand conditionally and skipped it.","solutions":["Null-check both operands at the call site and fail or default to a valid matrix.","Use Objects.requireNonNull(matrixA, \"matrixA\") to fail earlier with context.","Ensure deserializers return empty arrays instead of null for absent fields."],"exampleFix":"// before\ndouble[][] r = MatrixMultiplication.multiply(a, b);\n\n// after\nObjects.requireNonNull(a, \"matrixA\");\nObjects.requireNonNull(b, \"matrixB\");\ndouble[][] r = MatrixMultiplication.multiply(a, b);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(matrixA, \"matrixA\");\nObjects.requireNonNull(matrixB, \"matrixB\");\ndouble[][] r = MatrixMultiplication.multiply(matrixA, matrixB);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use Objects.requireNonNull at the boundary so the failure points at the right argument.","Configure deserializers to emit empty arrays rather than null for absent matrix fields."],"tags":["matrix","linear-algebra","null-check","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}