1

I am seeing following errors :

Object reference not set to an instance of an object!

Check to determinate if the object is null before calling the method!

I made a small test program for Sorted Linked Lists. Here is the code where the error comes!

    public void Insert(double data)
    {
        Link newLink = new Link(data);
        Link current = first;
        Link previous = null;

        if (first == null)
        {
            first = newLink;
        }
        
        else
        {
            while (data > current.DData && current != null)
            {
                previous = current;
                current = current.Next;
            }

            previous.Next = newLink;
            newLink.Next = current;
        }
    }

It says that the current referenc is null while (data > current.DData && current != null), but I assigned it: current = first;

The rest is the complete code of the Program!

class Link
{
    double dData;
    Link next=null;

    public Link Next
    {
        get { return next; }
        set { next = value; }
    }

    public double DData
    {
        get { return dData; }
        set { dData = value; }
    }

    public Link(double dData)
    {
        this.dData = dData;
    }


    public void DisplayLink()
    { 
    Console.WriteLine("Link : "+ dData);
    }

}

class SortedList
{
    Link first;

    public SortedList()
    { 
        first = null;
    }

    public bool IsEmpty()
    {
        return (this.first == null);
    }

    public void Insert(double data)
    {
        Link newLink = new Link(data);
        Link current = first;
        Link previous = null;

        if (first == null)
        {
            first = newLink;
        }
        
        else
        {
            while (data > current.DData && current != null)
            {
                previous = current;
                current = current.Next;
            }

            previous.Next = newLink;
            newLink.Next = current;
        }
    }

    public Link Remove()
    {
        Link temp = first;
        first = first.Next;
        return temp;
    }

    public void DisplayList()
    {
        Link current;
        current = first;

        Console.WriteLine("Display the List!");
        
        while (current != null)
        {
            current.DisplayLink();
            current = current.Next;
        }
    }
}

class SortedListApp
{
    public void TestSortedList()
    {
        SortedList newList = new SortedList();
        newList.Insert(20);
        newList.Insert(22);
        newList.Insert(100);
        newList.Insert(1000);
        newList.Insert(15);
        newList.Insert(11);

        newList.DisplayList();
        newList.Remove();
        newList.DisplayList();

    }
}
3
  • 1
    How do you know that first isn't null? (It seems to me that in an empty list it would be.) Commented Oct 25, 2012 at 22:58
  • 1
    Also, conditions like data > current.DData && current != null are short-circuiting from left to right. That means that data > current.DDAta is evaluated before current != null. The consequence is that if current is null, the combined expression will crash despite the current != null check, because the latter happens too late. Commented Oct 25, 2012 at 23:00
  • while (current != null && data > current.DData) Commented Oct 25, 2012 at 23:42

2 Answers 2

1

You are perhaps assuming that the while loop is breaking on the first iteration which it's not it's the assignment in the while loop that eventually breaks it.

Eventually current is NULL based on your code, you even test for it - change it to this and it should be fine:

while (current != null && data > current.DData)
{
    previous = current;
    current = current.Next;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks now I realize what the problem was, and now it sorts the list Fine!
no worries, could you please mark the answer as correct (if it is indeed correct). Cheers Rob
1

Agree that you have done

current = first; 

but then the beginning of your class first is null

class SortedList
{
    Link first;

please assign something to first else it will be null

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.