{"record":{"id":"610b8bdec8d6dc4a","repo":"TheAlgorithms/C-Sharp","slug":"argumentnullexception-graph","errorCode":null,"errorMessage":"ArgumentNullException: graph","messagePattern":"ArgumentNullException: graph","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Algorithms/Graph/Dijkstra/DijkstraAlgorithm.cs","lineNumber":87,"sourceCode":"        Vertex<T> startVertex)\n    {\n        var distArray = new DistanceModel<T>[graph.Count];\n\n        distArray[startVertex.Index] = new DistanceModel<T>(startVertex, startVertex, 0);\n\n        foreach (var vertex in graph.Vertices.Where(x => x != null && !x.Equals(startVertex)))\n        {\n            distArray[vertex!.Index] = new DistanceModel<T>(vertex, null, double.MaxValue);\n        }\n\n        return distArray;\n    }\n\n    private static void ValidateGraphAndStartVertex<T>(DirectedWeightedGraph<T> graph, Vertex<T> startVertex)\n    {\n        if (graph is null)\n        {\n            throw new ArgumentNullException(nameof(graph));\n        }\n\n        if (startVertex.Graph != null && !startVertex.Graph.Equals(graph))\n        {\n            throw new ArgumentNullException(nameof(graph));\n        }\n    }\n}\n","sourceCodeStart":69,"sourceCodeEnd":96,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Graph/Dijkstra/DijkstraAlgorithm.cs#L69-L96","documentation":"DijkstraAlgorithm.ValidateGraphAndStartVertex throws ArgumentNullException for 'graph' when the DirectedWeightedGraph passed to GenerateShortestPath is null. Dijkstra needs a concrete graph to relax edges, so a null graph is rejected up front (DijkstraAlgorithm.cs:87).","triggerScenarios":"Calling GenerateShortestPath(graph, startVertex, ...) with graph == null, typically when the graph variable comes from a builder or factory that returned null.","commonSituations":"A graph-building method returning null on failed parsing, deserializing a null graph from config/storage, or an uninitialized class field holding the graph reference.","solutions":["Ensure the graph is constructed (new DirectedWeightedGraph<T>(...) or builder output) before calling GenerateShortestPath.","Fix the factory/deserialization path that returned null instead of a graph instance.","Add a caller-side null check with a descriptive exception message identifying where the null originated."],"exampleFix":"// before\nvar graph = LoadGraph(path); // may return null\nvar path = DijkstraAlgorithm.GenerateShortestPath(graph, start);\n// after\nvar graph = LoadGraph(path) ?? throw new InvalidOperationException($\"Graph could not be loaded from {path}\");\nvar path = DijkstraAlgorithm.GenerateShortestPath(graph, start);","handlingStrategy":"validation","validationCode":"if (graph is null)\n    throw new InvalidOperationException(\"Shortest-path requested before the graph was built\");\nvar path = DijkstraAlgorithm.GenerateShortestPath(graph, startVertex);","typeGuard":"static bool HasGraph<T>(DirectedWeightedGraph<T>? graph) => graph is not null;","tryCatchPattern":"try\n{\n    var path = DijkstraAlgorithm.GenerateShortestPath(graph, startVertex);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"graph\")\n{\n    logger.LogError(ex, \"Graph was null when running Dijkstra\");\n    throw new InvalidOperationException(\"Graph must be built before GenerateShortestPath\", ex);\n}","preventionTips":["Make graph construction mandatory before exposing any path-finding API (factory guarantees non-null).","Avoid methods that return null graphs; throw or return a result type instead.","Enable nullable reference types to catch null graph fields at compile time."],"tags":["null-argument","csharp","dijkstra","shortest-path"],"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"}