{"record":{"id":"18cce764dc75c088","repo":"TheAlgorithms/Java","slug":"number-of-nodes-must-be-positive","errorCode":null,"errorMessage":"Number of nodes must be positive","messagePattern":"Number of nodes must be positive","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java","lineNumber":186,"sourceCode":"                    sb.append(\"Parent \").append(parent[i]);\n                }\n                sb.append(\"\\n\");\n            }\n            return sb.toString();\n        }\n    }\n\n    /**\n     * Creates a centroid tree from an edge list.\n     *\n     * @param n number of nodes (0-indexed: 0 to n-1)\n     * @param edges list of edges where each edge is [u, v]\n     * @return CentroidTree object\n     * @throws IllegalArgumentException if n &lt;= 0 or edges is invalid\n     */\n    public static CentroidTree buildFromEdges(int n, int[][] edges) {\n        if (n <= 0) {\n            throw new IllegalArgumentException(\"Number of nodes must be positive\");\n        }\n        if (edges == null) {\n            throw new IllegalArgumentException(\"Edges cannot be null\");\n        }\n        if (edges.length != n - 1) {\n            throw new IllegalArgumentException(\"Tree must have exactly n-1 edges\");\n        }\n\n        List<List<Integer>> adj = new ArrayList<>();\n        for (int i = 0; i < n; i++) {\n            adj.add(new ArrayList<>());\n        }\n\n        for (int[] edge : edges) {\n            if (edge.length != 2) {\n                throw new IllegalArgumentException(\"Each edge must have exactly 2 nodes\");\n            }\n            int u = edge[0];","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java#L168-L204","documentation":"Thrown by CentroidDecomposition.buildFromEdges when n is zero or negative. A tree must have at least one node and exactly n-1 edges; the method allocates arrays of size n, so a non-positive n is invalid. The IllegalArgumentException fails fast before any allocation or edge processing.","triggerScenarios":"Calling buildFromEdges(0, edges) or buildFromEdges with a negative count. Passing a node count derived from an empty input set. Passing a computed n that underflows due to an off-by-one.","commonSituations":"Graph input where the vertex count is missing or parsed as 0. Test fixtures with degenerate inputs. Logic that computes n = edges + 1 on an empty edge list yielding n=1 but mis-set to 0.","solutions":["Ensure n >= 1 before calling; reject empty graphs at the input boundary.","Compute n from the actual vertex set rather than a separate possibly-wrong counter.","Validate parsed configuration before it reaches the builder.","Handle the n>=1 requirement explicitly in your domain model."],"exampleFix":"// before\nCentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges);\n// after\nif (n <= 0) {\n    throw new IllegalArgumentException(\"Graph must have at least one node\");\n}\nCentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges);","handlingStrategy":"validation","validationCode":"if (n > 0) {\n    CentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reject empty graphs (n<=0) at the input boundary.","Derive n from the actual vertex set, not a separate counter.","Validate parsed node counts before building."],"tags":["tree","invalid-argument","java","datastructures","centroid-decomposition","precondition"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}