TheAlgorithms/C-Sharp · error · ArgumentNullException

ArgumentNullException: getNeighbors

Error message

ArgumentNullException: getNeighbors

What it means

Bridges.Find requires a delegate that yields the neighbors of each vertex. When getNeighbors is null the algorithm cannot traverse the graph, so ArgumentNullException(nameof(getNeighbors)) is thrown at Algorithms/Graph/Bridges.cs:31. Both parameter guards run before any graph work begins.

Solutions

  1. Pass an actual neighbor-accessor lambda, e.g. v => adjacency[v].
  2. Ensure any delegate variable holding the accessor is initialized before the call.
  3. Coalesce with a default accessor that throws a clearer message or returns empty neighbors if that is semantically valid.

Example fix

// before
Func<int, IEnumerable<int>> neighbors = null;
var bridges = Bridges.Find(vertices, neighbors);
// after
var bridges = Bridges.Find(vertices, v => adjacencyList[v]);
Defensive patterns

Strategy: validation

Validate before calling

if (getNeighbors is null)
    throw new ArgumentException("Neighbor accessor must not be null", nameof(getNeighbors));
var bridges = Bridges.Find(vertices, getNeighbors);

Type guard

static bool HasNeighborAccessor<T>(Func<T, IEnumerable<T>?>? accessor) => accessor is not null;

Try / catch

try
{
    var bridges = Bridges.Find(vertices, getNeighbors);
}
catch (ArgumentNullException ex) when (ex.ParamName == "getNeighbors")
{
    logger.LogError(ex, "Neighbor accessor delegate was null");
}

Prevention

When it happens

Trigger: Calling Bridges.Find<T>(vertices, null), e.g. Find(vertices, someOptionalDelegate) where the delegate variable was never assigned or a conditional expression returned null.

Common situations: Storing the neighbor accessor in a nullable field/property that was not initialized, reflection-based construction skipping the delegate, or refactoring that removed the lambda but left the call site compiling via a null-typed argument.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/df87a48cabb14558. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Graph/Bridges.cs:31

    /// <summary>
    /// Finds all bridges in an undirected graph.
    /// </summary>
    /// <typeparam name="T">Type of vertex.</typeparam>
    /// <param name="vertices">All vertices in the graph.</param>
    /// <param name="getNeighbors">Function to get neighbors of a vertex.</param>
    /// <returns>Set of bridges as tuples of vertices.</returns>
    public static HashSet<(T From, T To)> Find<T>(
        IEnumerable<T> vertices,
        Func<T, IEnumerable<T>> getNeighbors) where T : notnull
    {
        if (vertices == null)
        {
            throw new ArgumentNullException(nameof(vertices));
        }

        if (getNeighbors == null)
        {
            throw new ArgumentNullException(nameof(getNeighbors));
        }

        var vertexList = vertices.ToList();
        if (vertexList.Count == 0)
        {
            return new HashSet<(T, T)>();
        }

        var bridges = new HashSet<(T, T)>();
        var visited = new HashSet<T>();
        var discoveryTime = new Dictionary<T, int>();
        var low = new Dictionary<T, int>();
        var parent = new Dictionary<T, T?>();
        var time = 0;

        foreach (var vertex in vertexList)
        {
            if (!visited.Contains(vertex))

View on GitHub (pinned to 96e2905cab)