You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用邻接表实现Graph类时添加边抛出System.NullReferenceException求助

邻接表实现Graph类时System.NullReferenceException错误的解决方法

抛出的错误

System.NullReferenceException has been thrown

问题代码片段

MyNode类

class node
{
    public List<object>? Neighbors { get; set; }
    public int Data { get; set; }

    public node() { }
    public node(int val)
    {
        Data = val;
    }
}

Graph类

class Graph
{
    // Attributes of the Class
    public int NumVertices { get; set; }       // Number of vertices
    public List<node> vertices { get; set; }           // list of nodes in the graph

    // Graph constructor
    public Graph(int size)
    {
        NumVertices = size;
        vertices = new List<node>();
        //// Allocate node objects at each index of the vertice list
        //for (int i = 0; i < NumVertices; i++)
        //{
        //    Instantiate(vertices[i]); 
        //}
    }

    //Adding ege to vertice
    public void addEdge(node a, node b)
    {
        a.Neighbors.Add(b);   //---&gt; it referenced here
        b.Neighbors.Add(a);
    }
}

测试函数

public static void TestGraph()
{
    node a = new node(1);
    node b = new node(2);
    node c = new node(3);
    node d = new node(4);
    node e = new node(5);
    node f = new node(6);

    Graph myGraph = new Graph(6) ;
    //// Add vertices to the node
    //myGraph.vertices.Add(a);
    //myGraph.vertices.Add(b);
    //myGraph.vertices.Add(c);
    //myGraph.vertices.Add(d);
    //myGraph.vertices.Add(e);
    ////myGraph.vertices.Add(f);

    myGraph.addEdge(a, b);   //---&gt; Error originates here
    myGraph.addEdge(a, c);
    myGraph.addEdge(a, d);
    myGraph.addEdge(b, c);
    myGraph.addEdge(c, e);
    myGraph.addEdge(d, e);

    Console.WriteLine("Cheching if a and b are neighbors " + myGraph.isAdjacent(a, b).ToString());
    Console.WriteLine("Cheching if a and c are neighbors " + myGraph.isAdjacent(a, c).ToString());
}

错误原因

  1. Node类的Neighbors未初始化:Node类的Neighbors属性声明为可空的List<object>,但构造函数中未创建实例,默认值为null。调用addEdge时对null对象执行Add方法,触发空引用异常。
  2. Neighbors类型不匹配:Neighbors用于存储节点对象,却声明为List<object>,类型逻辑不合理,后续易引发其他问题。
  3. Graph未管理节点:测试代码中创建的节点未添加到Graph的vertices列表,Graph无法追踪所有节点,不符合邻接表的设计逻辑。

解决方法

1. 修复Node类

初始化Neighbors列表,并将类型改为List<node>以匹配存储对象:

class node
{
    public List<node> Neighbors { get; set; }
    public int Data { get; set; }

    public node() 
    {
        Neighbors = new List<node>();
    }
    public node(int val)
    {
        Data = val;
        Neighbors = new List<node>();
    }
}

2. 修复测试函数

取消注释添加节点到Graph的代码,让Graph管理所有节点:

public static void TestGraph()
{
    node a = new node(1);
    node b = new node(2);
    node c = new node(3);
    node d = new node(4);
    node e = new node(5);
    node f = new node(6);

    Graph myGraph = new Graph(6) ;
    // Add vertices to the node
    myGraph.vertices.Add(a);
    myGraph.vertices.Add(b);
    myGraph.vertices.Add(c);
    myGraph.vertices.Add(d);
    myGraph.vertices.Add(e);
    myGraph.vertices.Add(f);

    myGraph.addEdge(a, b);
    myGraph.addEdge(a, c);
    myGraph.addEdge(a, d);
    myGraph.addEdge(b, c);
    myGraph.addEdge(c, e);
    myGraph.addEdge(d, e);

    Console.WriteLine("Checking if a and b are neighbors " + myGraph.isAdjacent(a, b).ToString());
    Console.WriteLine("Checking if a and c are neighbors " + myGraph.isAdjacent(a, c).ToString());
}

3. 可选优化Graph构造函数

可以在Graph构造函数中直接创建节点,避免外部手动创建:

public Graph(int size)
{
    NumVertices = size;
    vertices = new List<node>();
    for (int i = 0; i < NumVertices; i++)
    {
        vertices.Add(new node(i + 1)); // 按顺序创建带Data的节点
    }
}

对应测试函数可简化为:

public static void TestGraph()
{
    Graph myGraph = new Graph(6);
    node a = myGraph.vertices[0];
    node b = myGraph.vertices[1];
    node c = myGraph.vertices[2];
    node d = myGraph.vertices[3];
    node e = myGraph.vertices[4];
    node f = myGraph.vertices[5];

    myGraph.addEdge(a, b);
    // ... 其他addEdge调用
}

内容的提问来源于stack exchange,提问作者Grimmace_23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 10:03:34