TheAlgorithms/C-Sharp · error · ArgumentNullException

ArgumentNullException: graph

Error message

ArgumentNullException: graph

What it means

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).

Solutions

  1. Ensure the graph is constructed (new DirectedWeightedGraph<T>(...) or builder output) before calling GenerateShortestPath.
  2. Fix the factory/deserialization path that returned null instead of a graph instance.
  3. Add a caller-side null check with a descriptive exception message identifying where the null originated.

Example fix

// before
var graph = LoadGraph(path); // may return null
var path = DijkstraAlgorithm.GenerateShortestPath(graph, start);
// after
var graph = LoadGraph(path) ?? throw new InvalidOperationException($"Graph could not be loaded from {path}");
var path = DijkstraAlgorithm.GenerateShortestPath(graph, start);
Defensive patterns

Strategy: validation

Validate before calling

if (graph is null)
    throw new InvalidOperationException("Shortest-path requested before the graph was built");
var path = DijkstraAlgorithm.GenerateShortestPath(graph, startVertex);

Type guard

static bool HasGraph<T>(DirectedWeightedGraph<T>? graph) => graph is not null;

Try / catch

try
{
    var path = DijkstraAlgorithm.GenerateShortestPath(graph, startVertex);
}
catch (ArgumentNullException ex) when (ex.ParamName == "graph")
{
    logger.LogError(ex, "Graph was null when running Dijkstra");
    throw new InvalidOperationException("Graph must be built before GenerateShortestPath", ex);
}

Prevention

When it happens

Trigger: Calling GenerateShortestPath(graph, startVertex, ...) with graph == null, typically when the graph variable comes from a builder or factory that returned null.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at Algorithms/Graph/Dijkstra/DijkstraAlgorithm.cs:87

        Vertex<T> startVertex)
    {
        var distArray = new DistanceModel<T>[graph.Count];

        distArray[startVertex.Index] = new DistanceModel<T>(startVertex, startVertex, 0);

        foreach (var vertex in graph.Vertices.Where(x => x != null && !x.Equals(startVertex)))
        {
            distArray[vertex!.Index] = new DistanceModel<T>(vertex, null, double.MaxValue);
        }

        return distArray;
    }

    private static void ValidateGraphAndStartVertex<T>(DirectedWeightedGraph<T> graph, Vertex<T> startVertex)
    {
        if (graph is null)
        {
            throw new ArgumentNullException(nameof(graph));
        }

        if (startVertex.Graph != null && !startVertex.Graph.Equals(graph))
        {
            throw new ArgumentNullException(nameof(graph));
        }
    }
}

View on GitHub (pinned to 96e2905cab)