Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:12:31+00:00 2026-06-13T11:12:31+00:00

I am seeing following errors : Object reference not set to an instance of

  • 0

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

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:12:32+00:00Added an answer on June 13, 2026 at 11:12 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very infrequently seeing the following error logged on a website I manage: Arguments
I'm looking at some open source projects and I'm seeing the following: NSLog(@%s w=%f,
I keep seeing references to _internal in examples like the following: class Symbol {
In preparing to a OOP exam, I enjoyed seeing g++ compile the following code
There must be something obvious that I'm doing wrong, but I'm not seeing it.
I have the following listener set up for pagebeforechange (very similar to the jQuery
I'm not seeing the real error in my custom error page when using an
im using the following method [DllImport(kernel32.dll, SetLastError=true)] static extern int GetProcessId(IntPtr hWnd); to try
I have the following R code and am seeing odd behavior - when I
Say you have the following object: public class Address { public String Line1 {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.