{"record":{"id":"f54232c2b0340e21","repo":"TheAlgorithms/Java","slug":"capacity-matrix-must-not-be-null-or-empty-f54232","errorCode":null,"errorMessage":"Capacity matrix must not be null or empty","messagePattern":"Capacity matrix must not be null or empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/GomoryHuTree.java","lineNumber":56,"sourceCode":"            for (int v = 0; v < n; v++) {\n                if (v != s && parent[v] == t && res.reachable[v]) {\n                    parent[v] = s;\n                }\n            }\n\n            if (t != 0 && res.reachable[parent[t]]) {\n                parent[s] = parent[t];\n                parent[t] = s;\n                weight[s] = weight[t];\n                weight[t] = f;\n            }\n        }\n        return new int[][] {parent, weight};\n    }\n\n    private static void validateCapacityMatrix(int[][] cap) {\n        if (cap == null || cap.length == 0) {\n            throw new IllegalArgumentException(\"Capacity matrix must not be null or empty\");\n        }\n        final int n = cap.length;\n        for (int i = 0; i < n; i++) {\n            if (cap[i] == null || cap[i].length != n) {\n                throw new IllegalArgumentException(\"Capacity matrix must be square\");\n            }\n            for (int j = 0; j < n; j++) {\n                if (cap[i][j] < 0) {\n                    throw new IllegalArgumentException(\"Capacities must be non-negative\");\n                }\n            }\n        }\n    }\n\n    private static final class MaxFlowResult {\n        final int flow;\n        final boolean[] reachable;\n        MaxFlowResult(int flow, boolean[] reachable) {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/GomoryHuTree.java#L38-L74","documentation":"GomoryHuTree.validateCapacityMatrix throws this IllegalArgumentException when the capacity matrix passed to the public API is null or has zero rows. It is the first check in the private validator invoked before any tree construction.","triggerScenarios":"Calling the Gomory-Hu tree constructor/method with a null capacity array or an empty int[][] (length 0).","commonSituations":"Graph parsed from input that yielded no vertices. Matrix left uninitialized after a failed load. Guard removed during a refactor assuming upstream always provides data.","solutions":["Null/empty-check the matrix before invoking the Gomory-Hu method and return an empty result.","Ensure the graph build step produces at least one vertex.","Log when the matrix comes back empty from the loader to catch upstream bugs."],"exampleFix":"// before\nint[][] tree = GomoryHuTree.build(cap); // cap may be null\n\n// after\nif (cap == null || cap.length == 0) {\n    return new int[0][];\n}\nint[][] tree = GomoryHuTree.build(cap);","handlingStrategy":"validation","validationCode":"if (cap == null || cap.length == 0) {\n    return new int[0][]; // empty tree\n}","typeGuard":"boolean hasMatrix(int[][] cap) { return cap != null && cap.length > 0; }","tryCatchPattern":null,"preventionTips":["Handle empty graphs at the caller before invoking Gomory-Hu.","Ensure graph loaders return a non-null matrix.","Log empty-matrix cases to catch upstream issues."],"tags":["graph-algorithm","argument-validation","null-check","gomory-hu-tree"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}