TheAlgorithms/Java · error · IllegalArgumentException
Edges list must not be null or empty
Error message
Edges list must not be null or empty
What it means
Thrown by the BoruvkaAlgorithm.Graph constructor when the `edges` list is null or empty. Boruvka's algorithm builds a minimum spanning tree from edges, so an empty edge set is meaningless and the constructor refuses it; null is treated identically to guard against uninitialized inputs.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java:50
/**
* Represents the graph
*/
static class Graph {
final int vertex;
final List<Edge> edges;
/**
* Constructor for the graph
*
* @param vertex number of vertices
* @param edges list of edges
*/
Graph(final int vertex, final List<Edge> edges) {
if (vertex < 0) {
throw new IllegalArgumentException("Number of vertices must be positive");
}
if (edges == null || edges.isEmpty()) {
throw new IllegalArgumentException("Edges list must not be null or empty");
}
for (final var edge : edges) {
checkEdgeVertices(edge.src, vertex);
checkEdgeVertices(edge.dest, vertex);
}
this.vertex = vertex;
this.edges = edges;
}
}
/**
* Represents a subset for Union-Find operations
*/
private static class Component {
int parent;
int rank;
View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure the edges list is populated before construction; load/generate edges first
- If an empty graph is legitimately possible in your flow, short-circuit before constructing (Boruvka needs >=1 edge)
- Replace null with an explicit non-empty list or fail earlier at the data-source step
Example fix
// before
Graph g = new BoruvkaAlgorithm.Graph(n, edges);
// after
if (edges == null || edges.isEmpty()) {
throw new IllegalStateException("no edges loaded");
}
Graph g = new BoruvkaAlgorithm.Graph(n, edges); Defensive patterns
Strategy: validation
Validate before calling
if (edges == null || edges.isEmpty()) {
throw new IllegalArgumentException("edges must be non-empty");
} Try / catch
try {
new BoruvkaAlgorithm.Graph(v, edges);
} catch (IllegalArgumentException e) {
// handle empty/null edges
} Prevention
- Validate edge data at load time, not at construction time
- Treat an empty edge set as a data-loading error rather than passing it through
When it happens
Trigger: Constructing the Graph with `null` for edges, `Collections.emptyList()`, or a list filtered down to zero elements (e.g. `edges.stream().filter(...).toList()`).
Common situations: Loading edge data from a file/network that returned nothing; a filter that removes all edges; forgetting to populate the list before constructing the graph.
Related errors
- Adjacency list must not be null
- Adjacency list must not contain null sets
- Capacity matrix must not be null or empty
- IPv4 address is empty.
- Input cannot be null or empty
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/c385131273156cc1.
Report an issue: GitHub.