TheAlgorithms/Java · error · IllegalArgumentException

Adjacency list must not be null

Error message

Adjacency list must not be null

What it means

Thrown by BronKerbosch.findMaximalCliques(List<Set<Integer>> adjacency) when the adjacency list itself is null. The method reads adjacency.size() and indexes adjacency.get(u); a null list NPEs, so it is rejected up front. Message: 'Adjacency list must not be null'.

Source

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

 *
 * @author <a href="https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm">Wikipedia: Bron–Kerbosch algorithm</a>
 */
public final class BronKerbosch {

    private BronKerbosch() {
    }

    /**
     * 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);
                }
            }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Construct an explicit adjacency list (even if empty) before calling.
  2. Make the graph builder return an empty list for no-vertex graphs.
  3. Guard with Objects.requireNonNull at the boundary.

Example fix

// before
List<Set<Integer>> cliques = BronKerbosch.findMaximalCliques(adj);

// after
List<Set<Integer>> adj = adjIn != null ? adjIn : new ArrayList<>();
List<Set<Integer>> cliques = BronKerbosch.findMaximalCliques(adj);
Defensive patterns

Strategy: validation

Validate before calling

List<Set<Integer>> adj = adjacency != null ? adjacency : new ArrayList<>();
BronKerbosch.findMaximalCliques(adj);

Type guard

adjacency != null

Prevention

When it happens

Trigger: Passing null for the adjacency list; a graph-construction step that returns null on empty input; uninitialized graph field.

Common situations: Graph builders that return null for an empty graph; tests missing the adjacency fixture; deserialization that maps an absent field to null.

Related errors


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