{"record":{"id":"93b03f50de657e90","repo":"TheAlgorithms/Java","slug":"cost-matrix-must-have-at-least-1-column","errorCode":null,"errorMessage":"Cost matrix must have at least 1 column","messagePattern":"Cost matrix must have at least 1 column","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/HungarianAlgorithm.java","lineNumber":137,"sourceCode":"        Arrays.fill(assignment, -1);\n        int total = 0;\n        for (int i = 0; i < rows; i++) {\n            int j = matchColForRow[i];\n            if (j >= 0 && j < cols) {\n                assignment[i] = j;\n                total += cost[i][j];\n            }\n        }\n        return new Result(assignment, total);\n    }\n\n    private static void validate(int[][] cost) {\n        if (cost == null || cost.length == 0) {\n            throw new IllegalArgumentException(\"Cost matrix must not be null or empty\");\n        }\n        int c = cost[0].length;\n        if (c == 0) {\n            throw new IllegalArgumentException(\"Cost matrix must have at least 1 column\");\n        }\n        for (int i = 0; i < cost.length; i++) {\n            if (cost[i] == null || cost[i].length != c) {\n                throw new IllegalArgumentException(\"Cost matrix must be rectangular with equal row lengths\");\n            }\n            for (int j = 0; j < c; j++) {\n                if (cost[i][j] < 0) {\n                    throw new IllegalArgumentException(\"Costs must be non-negative\");\n                }\n            }\n        }\n    }\n}\n","sourceCodeStart":119,"sourceCodeEnd":151,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/HungarianAlgorithm.java#L119-L151","documentation":"HungarianAlgorithm.validate throws this IllegalArgumentException when the cost matrix has rows but the first row has zero columns (c == 0). It guards against a degenerate matrix that has no columns to assign workers to.","triggerScenarios":"Passing a matrix like new int[3][] where each row is empty, or new int[][]{{}}.","commonSituations":"Building rows as new int[0] when no tasks are configured. A column count derived from an empty task list. Matrix transposition logic that dropped columns.","solutions":["Ensure each row has at least one column before calling.","Treat an empty task set as a no-op assignment upstream.","Validate column count > 0 during matrix construction."],"exampleFix":"// before\nint[][] cost = workers.stream().map(w -> new int[0]).toArray(int[][]::new);\n\n// after\nif (tasks.isEmpty()) return new int[0];\nint[][] cost = buildCostMatrix(workers, tasks); // guarantees cols >= 1","handlingStrategy":"validation","validationCode":"if (cost.length > 0 && (cost[0] == null || cost[0].length == 0)) {\n    throw new IllegalArgumentException(\"Cost matrix needs at least 1 column\");\n}","typeGuard":"boolean hasColumns(int[][] cost) {\n    return cost != null && cost.length > 0 && cost[0] != null && cost[0].length > 0;\n}","tryCatchPattern":null,"preventionTips":["Validate column count > 0 during matrix construction.","Handle empty task sets as a no-op before calling.","Pad or reject zero-column matrices upstream."],"tags":["graph-algorithm","argument-validation","matrix-shape","assignment-problem"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}