TheAlgorithms/Java · error · IllegalArgumentException
Edges cannot be null
Error message
Edges cannot be null
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java:189
}
return sb.toString();
}
}
/**
* Creates a centroid tree from an edge list.
*
* @param n number of nodes (0-indexed: 0 to n-1)
* @param edges list of edges where each edge is [u, v]
* @return CentroidTree object
* @throws IllegalArgumentException if n <= 0 or edges is invalid
*/
public static CentroidTree buildFromEdges(int n, int[][] edges) {
if (n <= 0) {
throw new IllegalArgumentException("Number of nodes must be positive");
}
if (edges == null) {
throw new IllegalArgumentException("Edges cannot be null");
}
if (edges.length != n - 1) {
throw new IllegalArgumentException("Tree must have exactly n-1 edges");
}
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
for (int[] edge : edges) {
if (edge.length != 2) {
throw new IllegalArgumentException("Each edge must have exactly 2 nodes");
}
int u = edge[0];
int v = edge[1];
if (u < 0 || u >= n || v < 0 || v >= n) {View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before int[][] edges = maybeNullEdges; CentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges); // after int[][] edges = maybeNullEdges != null ? maybeNullEdges : new int[0][]; CentroidTree ct = CentroidDecomposition.buildFromEdges(n, edges);
Defensive patterns
Strategy: validation
Validate before calling
int[][] safeEdges = (edges != null) ? edges : new int[0][]; CentroidTree ct = CentroidDecomposition.buildFromEdges(n, safeEdges);
Type guard
java.util.Objects.requireNonNull(edges, "edges");
Prevention
- Pass empty arrays, not null, for 'no edges'.
- Fix producers to return non-null collections.
- Use Optional for nullable edge sources.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Tree cannot be empty or null
- Number of nodes must be positive
- Stack cannot be null
- Invalid node: {}
- Tree must have exactly n-1 edges
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/2c1578095994f810.
Report an issue: GitHub.