Home > Software engineering >  Trying to add a node to a graph
Trying to add a node to a graph

Time:09-04

I am implementing a Graph via adjacency list, and I am trying to add a node to a Graph class but I keep getting an error

System.NullReferenceException has been thrown

this is the bit of my code:

MyNode class

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

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

This is my graph class:

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);   //---> it referenced here
            b.Neighbors.Add(a);
        }

Then testing functions: this Instantiates node objects and

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);   //---> 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());

How would I be able to rectify this issues? Thanks

CodePudding user response:

  myGraph.addEdge(a, b);   //---> Error originates here

So, what is the value of a? Your posted code leaves it uninitialized.

CodePudding user response:

There is no problem with the node object but it's Neighbors field which is supposed to be a List but the constructor doesn't have any initialization for it.

If you change your constructors to initialize Neighbors, the null reference error will go away. Below is an example to how you can initialize the list.

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