{"record":{"id":"bea32e725a021173","repo":"TheAlgorithms/Java","slug":"the-input-matrix-cannot-have-null-or-empty-rows-bea32e","errorCode":null,"errorMessage":"The input matrix cannot have null or empty rows","messagePattern":"The input matrix cannot have null or empty rows","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java","lineNumber":37,"sourceCode":"    }\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\n    /**\n     * @brief Checks if the input matrix is a jagged matrix.\n     * Jagged matrix is a matrix where the number of columns in each row is not the same.","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/utils/MatrixUtil.java#L19-L55","documentation":"Thrown by MatrixUtil.validateInputMatrix when hasValidRows returns false, i.e., at least one row is null or has length 0. Matrix arithmetic indexes each row to a fixed column count, so a null or zero-length row breaks iteration. The helper short-circuits on the first offending row.","triggerScenarios":"Passing new double[n][] with uninitialized inner arrays, a matrix with a row set to null after construction, or a parser that produced an empty row array for a blank input line.","commonSituations":"Partial initialization of a 2D array, inconsistent parsed lines, or a transform that left a trailing null row.","solutions":["Allocate every inner array before the call.","Reject or skip input lines that yield null/empty rows during parsing.","Inspect matrix[i] for each i to locate the offending row."],"exampleFix":"// before\ndouble[][] m = new double[2][]; // inner arrays null\nMatrixUtil.validateInputMatrix(m);\n\n// after\ndouble[][] m = new double[2][3];\nMatrixUtil.validateInputMatrix(m);","handlingStrategy":"validation","validationCode":"for (double[] row : matrix) {\n    if (row == null || row.length == 0) {\n        throw new IllegalStateException(\"Matrix contains a null/empty row\");\n    }\n}","typeGuard":"static boolean allRowsValid(double[][] m) {\n    if (m == null) return false;\n    for (double[] row : m) if (row == null || row.length == 0) return false;\n    return true;\n}","tryCatchPattern":null,"preventionTips":["Allocate 2D arrays with new double[rows][cols].","Reject parsed lines that yield empty row arrays."],"tags":["matrix","precondition","null-row","linear-algebra"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}