{"record":{"id":"7e02c37507492051","repo":"TheAlgorithms/Java","slug":"the-input-matrix-cannot-be-empty","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/QRDecomposition.java","lineNumber":121,"sourceCode":"\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        }\n        return true;\n    }\n","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/matrix/QRDecomposition.java#L103-L139","documentation":"Thrown by QRDecomposition.validateInputMatrix when matrix.length == 0, i.e., the outer array exists but contains zero rows. QR decomposition operates on row/column data, so an empty matrix (0x0) has no diagonal to iterate over and no vectors to normalize. The check runs after the null check and before row validation.","triggerScenarios":"Passing new double[0][], a freshly allocated empty matrix from a constructor, or a filtered/collected matrix whose source produced no rows (e.g., an empty CSV file parsed into rows).","commonSituations":"Empty input dataset, an upstream filter that removed all rows, a test fixture that builds a matrix from an empty list via toArray(new double[0][]), or a default-initialized matrix before data is appended.","solutions":["Verify the data source actually contains rows before building the double[][].","If an empty matrix is a legitimate case in your domain, branch around the call instead of passing it through.","Log the matrix dimensions immediately before the call to confirm row count."],"exampleFix":"// before\nList<double[]> rows = readRows(file);\ndouble[][] m = rows.toArray(new double[0][]);\nQRDecomposition.decompose(m);\n\n// after\nList<double[]> rows = readRows(file);\nif (rows.isEmpty()) {\n    throw new IllegalStateException(\"Input file contained no matrix rows\");\ndouble[][] m = rows.toArray(new double[0][]);\nQRDecomposition.decompose(m);","handlingStrategy":"validation","validationCode":"if (matrix == null || matrix.length == 0) {\n    throw new IllegalStateException(\"QR decomposition needs a non-empty matrix\");\n}","typeGuard":"static boolean isNonEmptyMatrix(double[][] m) {\n    return m != null && m.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Treat an empty matrix as a domain decision, not an algorithm input: branch before calling.","Log matrix dimensions right before decomposition 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"}