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
- Pass an actual neighbor-accessor lambda, e.g. v => adjacency[v].
- Ensure any delegate variable holding the accessor is initialized before the call.
- 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
- Keep the neighbor accessor as a non-nullable required constructor/parameter, not an optional field.
- Pass inline lambdas at the call site instead of storing delegates in nullable variables.
- Unit-test graph helper wiring so a missing accessor fails fast in tests, not production.
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
- ArgumentNullException: vertices
- ArgumentNullException: graph
- ArgumentNullException: features
- Input data cannot be null.
- The parameters 'listOfAs' and 'listOfNs' must not be null…
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)