TheAlgorithms/Java · error · IllegalArgumentException

Adjacency list must not contain null sets

Error message

Adjacency list must not contain null sets

What it means

Thrown by BronKerbosch.findMaximalCliques when any entry adjacency.get(u) is null. The method iterates each vertex's neighbor set; a null set NPEs on iteration. Message: 'Adjacency list must not contain null sets'.

Source

Thrown at src/main/java/com/thealgorithms/graph/BronKerbosch.java:41

    /**
     * Finds all maximal cliques of the provided graph.
     *
     * @param adjacency adjacency list where {@code adjacency.size()} equals the number of vertices
     * @return a list containing every maximal clique, each represented as a {@link Set} of vertices
     * @throws IllegalArgumentException if the adjacency list is {@code null}, contains {@code null}
     *         entries, or references invalid vertices
     */
    public static List<Set<Integer>> findMaximalCliques(List<Set<Integer>> adjacency) {
        if (adjacency == null) {
            throw new IllegalArgumentException("Adjacency list must not be null");
        }

        int n = adjacency.size();
        List<Set<Integer>> graph = new ArrayList<>(n);
        for (int u = 0; u < n; u++) {
            Set<Integer> neighbors = adjacency.get(u);
            if (neighbors == null) {
                throw new IllegalArgumentException("Adjacency list must not contain null sets");
            }
            Set<Integer> copy = new HashSet<>();
            for (int v : neighbors) {
                if (v < 0 || v >= n) {
                    throw new IllegalArgumentException("Neighbor index out of bounds: " + v);
                }
                if (v != u) {
                    copy.add(v);
                }
            }
            graph.add(copy);
        }

        Set<Integer> r = new HashSet<>();
        Set<Integer> p = new HashSet<>();
        Set<Integer> x = new HashSet<>();
        for (int v = 0; v < n; v++) {
            p.add(v);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Represent every vertex's neighbor set, using an empty Set for isolated vertices.
  2. Normalize the adjacency list: replace null entries with empty sets before calling.
  3. Build the list with a fixed size and fill every slot.

Example fix

// before
List<Set<Integer>> adj = Arrays.asList(null, Set.of(1,2));

// after
List<Set<Integer>> adj = new ArrayList<>();
adj.add(new HashSet<>()); // vertex 0 isolated
adj.add(new HashSet<>(Set.of(0,2)));
Defensive patterns

Strategy: validation

Validate before calling

for (int u = 0; u < adjacency.size(); u++) {
    if (adjacency.get(u) == null) adjacency.set(u, new HashSet<>());
}

Type guard

adjacency.stream().allMatch(Objects::nonNull)

Prevention

When it happens

Trigger: A vertex with no neighbors represented as null instead of an empty set; a list built with some slots left null; sparse adjacency where isolated vertices were skipped.

Common situations: Building adjacency by adding only connected vertices; deserializing a sparse map into a list with gaps; arrays.asList with null elements.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/9d19e8a1ac3159c2. Report an issue: GitHub.