TheAlgorithms/Java · error · IllegalArgumentException

Vertex {vertex} is out of bounds

Error message

Vertex {vertex} is out of bounds

What it means

Thrown by `WelshPowell.Graph.validateVertex` when a vertex index is `< 0` or `>= getNumVertices()`. Every vertex passed to `addEdge` must index an existing vertex; the guard runs for both endpoints. It protects the `adjacencyLists` array from out-of-bounds access.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java:78

        private void addEdge(int nodeA, int nodeB) {
            validateVertex(nodeA);
            validateVertex(nodeB);
            if (nodeA == nodeB) {
                throw new IllegalArgumentException("Self-loops are not allowed");
            }
            adjacencyLists[nodeA].add(nodeB);
            adjacencyLists[nodeB].add(nodeA);
        }

        /**
         * Validates that the vertex index is within the bounds of the graph.
         *
         * @param vertex the index of the vertex to validate
         * @throws IllegalArgumentException if the vertex is out of bounds
         */
        private void validateVertex(int vertex) {
            if (vertex < 0 || vertex >= getNumVertices()) {
                throw new IllegalArgumentException("Vertex " + vertex + " is out of bounds");
            }
        }

        /**
         * Returns the adjacency list for a specific vertex.
         *
         * @param vertex the index of the vertex
         * @return the set of adjacent vertices
         */
        HashSet<Integer> getAdjacencyList(int vertex) {
            return adjacencyLists[vertex];
        }

        /**
         * Returns the number of vertices in the graph.
         *
         * @return the number of vertices
         */

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure all vertex indices are in `[0, numberOfVertices)`
  2. Convert 1-based data to 0-based
  3. Set `numberOfVertices` to at least `max(all endpoints) + 1`

Example fix

// before (index 4 exceeds count 3)
WelshPowell.makeGraph(3, new int[][]{{0, 4}});
// after (count covers index 4)
WelshPowell.makeGraph(5, new int[][]{{0, 4}});
Defensive patterns

Strategy: validation

Validate before calling

for (int[] e : edges) {
    for (int v : e) {
        if (v < 0 || v >= numberOfVertices) {
            throw new IllegalArgumentException("vertex " + v + " out of bounds");
        }
    }
}

Try / catch

try {
    WelshPowell.makeGraph(n, edges);
} catch (IllegalArgumentException e) {
    // out-of-bounds vertex
}

Prevention

When it happens

Trigger: Calling `makeGraph` (or `addEdge`) with a vertex `>= numberOfVertices` or negative — e.g. a 1-based index, or an edge referencing a vertex beyond the declared count.

Common situations: 1-based vs 0-based confusion; vertex count smaller than the largest referenced vertex; edges built for a larger graph.

Related errors


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