{"record":{"id":"2c1578095994f810","repo":"TheAlgorithms/Java","slug":"edges-cannot-be-null","errorCode":null,"errorMessage":"Edges cannot be null","messagePattern":"Edges cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java","lineNumber":189,"sourceCode":"            }\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];\n            int v = edge[1];\n\n            if (u < 0 || u >= n || v < 0 || v >= n) {","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java#L171-L207","documentation":"Thrown by CentroidDecomposition.buildFromEdges when the edges array is null. The method iterates over edges to build the adjacency list, so a null reference would NPE. The IllegalArgumentException is a fail-fast null check performed after the node-count validation.","triggerScenarios":"Passing null for the edges argument. Passing an edges field that was never populated by a parser. Passing the result of a lookup that returned null on absence.","commonSituations":"Input parsing that returns null instead of an empty array when no edges exist. Optional/map chains without a fallback. Deserialization yielding null for a missing field.","solutions":["Pass an empty array (new int[0][]) when there are no edges, not null.","Add a null check at the call site and substitute an empty edge set.","Fix the upstream producer to never emit null for the edges collection.","Use Collections.emptyList / empty arrays as the canonical 'no edges' representation."],"exampleFix":"// before\nint[][] edges = maybeNullEdges;\nCentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges);\n// after\nint[][] edges = maybeNullEdges != null ? maybeNullEdges : new int[0][];\nCentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges);","handlingStrategy":"validation","validationCode":"int[][] safeEdges = (edges != null) ? edges : new int[0][];\nCentroidTree ct = CentroidDecomposition.buildFromEdges(n, safeEdges);","typeGuard":"java.util.Objects.requireNonNull(edges, \"edges\");","tryCatchPattern":null,"preventionTips":["Pass empty arrays, not null, for 'no edges'.","Fix producers to return non-null collections.","Use Optional for nullable edge sources."],"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"}