Guest
Guest

Reputation: 33

Object reference not set to an instance of an object c#

I have the following generic classes (Node and List based on Nodes):

public class Node<T>
    {
        private T info;
        private Node<T> next;

        public Node(T x, Node<T> next)
        {
            this.info = x;
            this.next = next;
        }

        public Node<T> GetNext()
        {
            return this.next;
        }

        public void SetNext(Node<T> next)
        {
            this.next = next;
        }

        public T GetInfo()
        {
            return this.info;
        }

        public void SetInfo(T x)
        {
            this.info = x;
        }

        public override string ToString()
        {
            return this.info.ToString();
        }
    }

    public class List<T>
    {

        private Node<T> first;

        public List()
        {
            this.first = null;
        }

        public Node<T> GetFirst()
        {
            return this.first;
        }

        public Node<T> Add(Node<T> pos, T x)
        {
            Node<T> temp = new Node<T>(x, null);

            if (pos == null)
            {
                temp.SetNext(this.first);
                this.first = temp;
            }
            else
            {
                temp.SetNext(pos.GetNext());
                pos.SetNext(temp);
            }

            return temp;
        }

        public Node<T> Remove(Node<T> pos)
        {
            if (this.first == pos)
                this.first = pos.GetNext();
            else
            {
                Node<T> prevPos = this.GetFirst();
                while (prevPos.GetNext() != pos)
                    prevPos = prevPos.GetNext();
                prevPos.SetNext(pos.GetNext());
            }

            Node<T> nextPos = pos.GetNext();
            pos.SetNext(null);

            return nextPos;
        }


        public override string ToString()
        {
            string str = "[";
            Node<T> pos = this.first;
            while (pos != null)
            {
                str += pos.GetInfo().ToString();
                if (pos.GetNext() != null)
                    str += ", ";
                pos = pos.GetNext();
            }

            str += "]";

            return str;
        }
    }

What i want to do is two in-class functions for List class. - The first function will sum up all the numbers in the List which are at even position. - The second function will check up whether the List is sorted in alphabetical order.

What I've done:

   public int SumElementsOnEven()
    {
        int sum = 0;
        Node<T> pos = this.first;
        while (pos != null)
        {
            sum += int.Parse(pos.GetInfo().ToString());
            pos = pos.GetNext().GetNext();
        }
        return sum;
    }

    public bool isAlpha()
    {
        Node<T> pos = this.first;
        while (pos != null)
        {
            string a = pos.GetInfo().ToString();
            string b = pos.GetNext().GetInfo().ToString();
            if (a[0] < b[0])
                return false;
            pos = pos.GetNext();
        }

        return true;
    }

But it returns error in both of them.

Object reference not set to an instance of an object.

In the place where i do twice:

GetNext().GetNext();

This skipping one more element. And the second error in the place:

string b = pos.GetNext().GetInfo().ToString();

Because i want to compare two elements. Basically it's like saving reference from reference or maybe I'm wrong? Anyways, How can i solve this?

Upvotes: 2

Views: 717

Answers (2)

Hogan
Hogan

Reputation: 70523

You are getting this error because there is no next element. You need to change your code to check for nulls.

For example:

if (GetNext() != null)
  // do something here!
else
  // deal with case where there is no next here.

Upvotes: 0

The Scrum Meister
The Scrum Meister

Reputation: 30111

pos.GetNext() will return null when pos is the last node in the list, so you need to check before calling .GetNext() on a null reference:

while (pos != null)
{
    sum += int.Parse(pos.GetInfo().ToString());
    pos = pos.GetNext();
    if(pos != null)
        pos = pos.GetNext();
}

Same thing in the isAlpha() method:

while (pos != null)
{
    string a = pos.GetInfo().ToString();
    pos = pos.GetNext();
    if(pos == null)
        return true;
    string b = pos.GetInfo().ToString();
    if (a[0] < b[0])
        return false;
}

Upvotes: 3

Related Questions