{"record":{"id":"4352fdde4e1da215","repo":"TheAlgorithms/Java","slug":"tree-cannot-be-empty-or-null","errorCode":null,"errorMessage":"Tree cannot be empty or null","messagePattern":"Tree cannot be empty or null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java","lineNumber":50,"sourceCode":"     * Represents the centroid tree structure.\n     */\n    public static final class CentroidTree {\n        private final int n;\n        private final List<List<Integer>> adj;\n        private final int[] parent;\n        private final int[] subtreeSize;\n        private final boolean[] removed;\n        private int root;\n\n        /**\n         * Constructs a centroid tree from an adjacency list.\n         *\n         * @param adj adjacency list representation of the tree (0-indexed)\n         * @throws IllegalArgumentException if tree is empty or null\n         */\n        public CentroidTree(List<List<Integer>> adj) {\n            if (adj == null || adj.isEmpty()) {\n                throw new IllegalArgumentException(\"Tree cannot be empty or null\");\n            }\n\n            this.n = adj.size();\n            this.adj = adj;\n            this.parent = new int[n];\n            this.subtreeSize = new int[n];\n            this.removed = new boolean[n];\n            Arrays.fill(parent, -1);\n\n            // Build centroid tree starting from node 0\n            this.root = decompose(0, -1);\n        }\n\n        /**\n         * Recursively builds the centroid tree.\n         *\n         * @param u current node\n         * @param p parent in centroid tree","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java#L32-L68","documentation":"Thrown by the CentroidTree constructor when the supplied adjacency list is null or empty. Centroid decomposition requires at least one node and recurses on adj, so a null/empty list would NPE or produce a meaningless tree. The IllegalArgumentException is a fail-fast guard at construction.","triggerScenarios":"Passing null for the adjacency list. Passing an empty List<List<Integer>>. Passing a list that was built conditionally and left empty when no nodes exist.","commonSituations":"Graph builders that return empty when the input graph has no vertices. Config/serialization that yields null for a missing graph section. Test scaffolding that constructs the tree before building edges.","solutions":["Ensure the adjacency list has at least one (possibly empty) entry per node before constructing.","Skip CentroidTree construction entirely when the graph is empty, handling that case separately.","Validate the upstream graph builder to never return null; return an empty structure or throw earlier.","Use buildFromEdges with n>=1 and valid edges instead of constructing directly from an adjacency list."],"exampleFix":"// before\nCentroidTree ct = new CentroidTree(adj);\n// after\nif (adj == null || adj.isEmpty()) {\n    throw new IllegalStateException(\"Graph has no nodes\");\n}\nCentroidTree ct = new CentroidTree(adj);","handlingStrategy":"validation","validationCode":"if (adj != null && !adj.isEmpty()) {\n    CentroidTree ct = new CentroidTree(adj);\n} else {\n    // handle empty graph separately\n}","typeGuard":"java.util.Objects.requireNonNull(adj, \"adjacency list\");","tryCatchPattern":null,"preventionTips":["Skip CentroidTree construction for empty graphs.","Ensure adjacency lists have one entry per node.","Prefer buildFromEdges with validated n and edges."],"tags":["tree","null-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"}