0

So to explain it, what i am trying is to Add a Person at the end of the linked list. But i'm not able to, due to an exception thrown, at the line: if(current.Next == null)

The exception is: "System.NullReferenceException: 'Object reference not set to an instance of an object.'"

Here is the code:

  public void Insert(Person p)
    {
        Node toBeInserted = new Node();
        toBeInserted.Data = p;
        if(count == 0)
        {
            head = new Node();
            head.Data = p;
            head.Next = null;
        }
        else
        {
            Node current = new Node();
            current = head.Next;
            if(current == null)
            {
                head.Next = toBeInserted;
            }
            else
            {
                do
                {
                    current = current.Next;
                    if(current.Next == null)
                    {
                        current = toBeInserted;
                        break;
                    }
                } while (current.Next != null);

            }
        }
        count++;
    }

If anybody could help me, by explaining perhaps why my code doesn't make sense and why i get the exception. Cheers

Mxt92
  • 9
  • 1

0 Answers0