{"record":{"id":"dc87422d8d163e3a","repo":"TheAlgorithms/Java","slug":"the-input-matrix-cannot-have-null-or-empty-rows","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/QRDecomposition.java","lineNumber":124,"sourceCode":"        for (int i = 0; i < v.length; i++) {\n            result[i] = v[i] * scalar;\n        }\n        return result;\n    }\n\n    private static double norm(double[] v) {\n        return Math.sqrt(dotProduct(v, v));\n    }\n\n    private 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    private static boolean isJaggedMatrix(double[][] matrix) {\n        int numColumns = matrix[0].length;\n        for (double[] row : matrix) {","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/QRDecomposition.java#L106-L142","documentation":"Thrown by QRDecomposition.validateInputMatrix when hasValidRows returns false, meaning at least one row is null or has length 0. QR decomposition indexes every row to the same column count, so a null or zero-length row breaks dot-product and normalization loops. The check uses a short-circuit helper that returns false on the first offending row.","triggerScenarios":"Passing new double[3][] (rows allocated but inner arrays null), a matrix where one row was set to null after construction, or a parsed matrix where one line of input was blank and produced an empty row array.","commonSituations":"Partial initialization (outer array sized but inner arrays not), mixed data lines with one empty record, or a copy/transform that left a trailing null row.","solutions":["Allocate inner arrays for every row before the call (new double[n][m]).","Sanitize parsed input: skip or reject lines that yield null/empty row arrays.","Inspect each matrix[i] in a debugger/log to find the offending index before the call."],"exampleFix":"// before\ndouble[][] m = new double[3][]; // inner arrays null\nm[0] = new double[]{1,2};\n// m[1] forgotten -> null\nQRDecomposition.decompose(m);\n\n// after\ndouble[][] m = new double[3][2]; // all rows allocated\nm[0][0]=1; m[0][1]=2; // ... fill remaining\nQRDecomposition.decompose(m);","handlingStrategy":"validation","validationCode":"for (double[] row : matrix) {\n    if (row == null || row.length == 0) {\n        throw new IllegalStateException(\"Matrix has 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 as new double[rows][cols] so all inner arrays are non-null.","Sanitize parsed input: skip or reject 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"}