{"record":{"id":"dba9b595dca06909","repo":"TheAlgorithms/Java","slug":"the-input-matrix-cannot-be-empty-dba9b5","errorCode":null,"errorMessage":"The input matrix cannot be empty","messagePattern":"The input matrix cannot be empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java","lineNumber":34,"sourceCode":"\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        }\n        return true;\n    }\n","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java#L16-L52","documentation":"Thrown by MatrixUtil.validateInputMatrix when matrix.length == 0 — the outer array exists but contains zero rows. Matrix operations need at least one row to determine column count and perform arithmetic; an empty matrix (0x0) is degenerate. This check follows the null check and precedes the row-validity check.","triggerScenarios":"Passing new double[0][], an empty matrix from a list-to-array conversion with no elements, or a parser that produced no rows from blank input.","commonSituations":"Empty input file, an upstream filter that removed all rows, or a default-initialized matrix before appending data.","solutions":["Verify the data source yields at least one row before building the matrix.","Branch around the operation if an empty matrix is legitimate in your domain.","Log matrix.length right before the call to confirm it is zero."],"exampleFix":"// before\ndouble[][] m = rows.toArray(new double[0][]);\nMatrixUtil.validateInputMatrix(m); // throws when rows empty\n\n// after\nif (rows.isEmpty()) throw new IllegalStateException(\"No rows to build matrix\");\ndouble[][] m = rows.toArray(new double[0][]);\nMatrixUtil.validateInputMatrix(m);","handlingStrategy":"validation","validationCode":"if (matrix == null || matrix.length == 0) {\n    throw new IllegalStateException(\"Matrix must have at least one row\");\n}","typeGuard":"static boolean isNonEmptyMatrix(double[][] m) {\n    return m != null && m.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Branch on empty matrices before calling matrix utilities.","Log matrix.length to catch empty inputs early."],"tags":["matrix","precondition","empty-input","linear-algebra"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}