{"record":{"id":"c6d80c14da0c732f","repo":"TheAlgorithms/Java","slug":"input-matrices-must-not-be-empty","errorCode":null,"errorMessage":"Input matrices must not be empty","messagePattern":"Input matrices must not be empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/MatrixMultiplication.java","lineNumber":44,"sourceCode":"    }\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];\n\n        // Perform matrix multiplication\n        for (int i = 0; i < rowsA; i++) {\n            for (int j = 0; j < colsB; j++) {\n                for (int k = 0; k < colsA; k++) {","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/MatrixMultiplication.java#L26-L62","documentation":"Thrown by MatrixMultiplication.multiply when either matrix has zero rows, or the first row of either matrix has zero columns (matrixA[0].length == 0 || matrixB[0].length == 0). An empty matrix has no defined product, so the guard fails fast instead of producing a zero-dimension result array. Note: this check accesses matrix[0] and will itself NPE if a row is null but the outer array is non-empty.","triggerScenarios":"Call multiply(new double[0][], b), multiply(a, new double[][]{{}}), or pass a matrix built from an empty list (new double[list.size()][] with list empty).","commonSituations":"Filtering/grouping that produced zero rows, CSV parsed into an empty 2D array for a header-only file, or a placeholder new double[0][0] used as a 'no data' sentinel.","solutions":["Check dimensions before calling and skip/handle the empty case explicitly.","Represent 'no matrix' as null and catch error 474 instead, or use Optional<double[][]>.","Ensure data loaders throw on empty input rather than returning an empty matrix silently."],"exampleFix":"// before\ndouble[][] r = MatrixMultiplication.multiply(a, b);\n\n// after\nif (a.length == 0 || b.length == 0 || a[0].length == 0 || b[0].length == 0) {\n    return new double[0][0]; // or throw a domain-specific exception\n}\ndouble[][] r = MatrixMultiplication.multiply(a, b);","handlingStrategy":"validation","validationCode":"if (matrixA.length == 0 || matrixB.length == 0 || matrixA[0].length == 0 || matrixB[0].length == 0) {\n    throw new IllegalArgumentException(\"matrices must be non-empty\");\n}\ndouble[][] r = MatrixMultiplication.multiply(matrixA, matrixB);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Represent 'no data' explicitly (sentinel or Optional) instead of an empty 2D array.","Have data loaders throw on empty input rather than silently returning new double[0][]."],"tags":["matrix","linear-algebra","empty-check","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}