{"record":{"id":"d74dd3a8875be0d0","repo":"TheAlgorithms/Java","slug":"the-input-matrix-cannot-be-null","errorCode":null,"errorMessage":"The input matrix cannot be null","messagePattern":"The input matrix cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/matrix/QRDecomposition.java","lineNumber":118,"sourceCode":"        }\n        return result;\n    }\n\n    private static double[] scalarMultiply(double[] v, double scalar) {\n        double[] result = new double[v.length];\n        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        }","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/QRDecomposition.java#L100-L136","documentation":"Thrown by QRDecomposition.validateInputMatrix when the matrix argument passed to the decomposition is the Java null reference. QR decomposition requires a real-valued matrix to factor into Q (orthogonal) and R (upper triangular), so a null matrix has no dimensions or rows to operate on and the algorithm cannot proceed. This is a precondition check that fires before any Householder/normalization work begins.","triggerScenarios":"Calling QRDecomposition with a double[][] variable that was never assigned (e.g., a field left null, a failed lookup that returns null, or a deserialized object missing its matrix). Any public QRDecomposition entry point that delegates to validateInputMatrix will throw on a null argument.","commonSituations":"Reading a matrix from a file/DB that returned null on missing data, refactoring that removed the assignment site, or passing the result of a method that returns null on error without checking.","solutions":["Ensure the double[][] passed to QRDecomposition is instantiated (e.g., new double[m][n]) before the call.","Check the upstream data source (file loader, parser, RPC) that produced the matrix and fix its null-return path.","Add an explicit null guard at the call site that supplies a sensible default or fails earlier with a clearer message."],"exampleFix":"// before\ndouble[][] m = loadMatrix(path); // may return null\nQRDecomposition.decompose(m);\n\n// after\ndouble[][] m = loadMatrix(path);\nif (m == null) {\n    throw new IllegalStateException(\"No matrix loaded from \" + path);\n}\nQRDecomposition.decompose(m);","handlingStrategy":"validation","validationCode":"if (matrix == null) {\n    throw new IllegalStateException(\"Matrix required for QR decomposition\");\n}\nQRDecomposition.decompose(matrix);","typeGuard":"static boolean isUsableMatrix(double[][] m) {\n    return m != null && m.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Never let a matrix-returning method return null; return an empty array instead and branch on size.","Add a null precondition check (Objects.requireNonNull) at the boundary where the matrix enters your code."],"tags":["matrix","null-check","precondition","linear-algebra"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}