{"record":{"id":"df87a48cabb14558","repo":"TheAlgorithms/C-Sharp","slug":"argumentnullexception-getneighbors","errorCode":null,"errorMessage":"ArgumentNullException: getNeighbors","messagePattern":"ArgumentNullException: getNeighbors","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Algorithms/Graph/Bridges.cs","lineNumber":31,"sourceCode":"    /// <summary>\n    /// Finds all bridges in an undirected graph.\n    /// </summary>\n    /// <typeparam name=\"T\">Type of vertex.</typeparam>\n    /// <param name=\"vertices\">All vertices in the graph.</param>\n    /// <param name=\"getNeighbors\">Function to get neighbors of a vertex.</param>\n    /// <returns>Set of bridges as tuples of vertices.</returns>\n    public static HashSet<(T From, T To)> Find<T>(\n        IEnumerable<T> vertices,\n        Func<T, IEnumerable<T>> getNeighbors) where T : notnull\n    {\n        if (vertices == null)\n        {\n            throw new ArgumentNullException(nameof(vertices));\n        }\n\n        if (getNeighbors == null)\n        {\n            throw new ArgumentNullException(nameof(getNeighbors));\n        }\n\n        var vertexList = vertices.ToList();\n        if (vertexList.Count == 0)\n        {\n            return new HashSet<(T, T)>();\n        }\n\n        var bridges = new HashSet<(T, T)>();\n        var visited = new HashSet<T>();\n        var discoveryTime = new Dictionary<T, int>();\n        var low = new Dictionary<T, int>();\n        var parent = new Dictionary<T, T?>();\n        var time = 0;\n\n        foreach (var vertex in vertexList)\n        {\n            if (!visited.Contains(vertex))","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Graph/Bridges.cs#L13-L49","documentation":"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.","triggerScenarios":"Calling Bridges.Find<T>(vertices, null), e.g. Find(vertices, someOptionalDelegate) where the delegate variable was never assigned or a conditional expression returned null.","commonSituations":"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.","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."],"exampleFix":"// before\nFunc<int, IEnumerable<int>> neighbors = null;\nvar bridges = Bridges.Find(vertices, neighbors);\n// after\nvar bridges = Bridges.Find(vertices, v => adjacencyList[v]);","handlingStrategy":"validation","validationCode":"if (getNeighbors is null)\n    throw new ArgumentException(\"Neighbor accessor must not be null\", nameof(getNeighbors));\nvar bridges = Bridges.Find(vertices, getNeighbors);","typeGuard":"static bool HasNeighborAccessor<T>(Func<T, IEnumerable<T>?>? accessor) => accessor is not null;","tryCatchPattern":"try\n{\n    var bridges = Bridges.Find(vertices, getNeighbors);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"getNeighbors\")\n{\n    logger.LogError(ex, \"Neighbor accessor delegate was null\");\n}","preventionTips":["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."],"tags":["null-argument","csharp","graph-algorithms"],"backgroundTag":"null-argument","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}