2

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?

2
  • Naming your own class List<T> is quite confusing since there is a List<T> is the BCL. Commented Mar 20, 2011 at 20:20
  • i rewrote this for practice sakes. Commented Mar 20, 2011 at 20:25

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much sir. It's pretty weird because i tried this before and it didn't work but now it magically works.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.