{"record":{"id":"b4bc55abc3998a4b","repo":"TheAlgorithms/Java","slug":"cost-matrix-must-not-be-null-or-empty","errorCode":null,"errorMessage":"Cost matrix must not be null or empty","messagePattern":"Cost matrix must not be null or empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/HungarianAlgorithm.java","lineNumber":133,"sourceCode":"        }\n\n        // Build assignment for original rows only, ignore padded rows\n        int[] assignment = new int[rows];\n        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":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/HungarianAlgorithm.java#L115-L151","documentation":"HungarianAlgorithm.validate throws this IllegalArgumentException when the cost matrix is null or has zero rows. It is the first guard in the validator that protects the assignment algorithm's row/column bookkeeping.","triggerScenarios":"Calling the Hungarian algorithm with null or an empty int[][] cost matrix.","commonSituations":"An empty assignment problem (no workers or no tasks). Matrix not built yet due to an earlier exception. Deserialization returning null.","solutions":["Handle null/empty as a special case (return an empty assignment) before calling.","Ensure at least one worker and one task exist before constructing the matrix.","Guard the upstream data source that produces the cost matrix."],"exampleFix":"// before\nHungarianAlgorithm.Result r = HungarianAlgorithm.solve(cost);\n\n// after\nif (cost == null || cost.length == 0) {\n    return new int[0];\n}\nHungarianAlgorithm.Result r = HungarianAlgorithm.solve(cost);","handlingStrategy":"validation","validationCode":"if (cost == null || cost.length == 0) {\n    return new int[0]; // no workers\n}","typeGuard":"boolean hasRows(int[][] cost) { return cost != null && cost.length > 0; }","tryCatchPattern":null,"preventionTips":["Short-circuit empty assignment problems at the caller.","Ensure at least one worker and one task before building the matrix.","Guard the data source that produces the cost matrix."],"tags":["graph-algorithm","argument-validation","null-check","assignment-problem"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}