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 6739275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:28:05+00:00 2026-05-26T11:28:05+00:00

class ListNode { public object Data { get; private set; } public ListNode Next

  • 0
 class ListNode
{
    public object Data { get; private set; }

    public ListNode Next { get; set; }

    public ListNode(object Element)
    {
        Data = Element;
    }

    public ListNode(object Element, ListNode NextNode)
    {
        Data = Element;
        Next = NextNode;
    }

    public ListNode()
    {

    }
}

 class LinkedList
{
    ListNode first;
    ListNode last;

    public LinkedList()
    {
        first = null;
        last = null;
    }


   public ListNode Find(object After)
    {
        ListNode current = new ListNode();
        current= first;
        while (current.Data != After)
        current = current.Next;
        return current;
    }

   public void Add(object newItem, object After)
   {
       ListNode current=new ListNode();
       ListNode newNode=new ListNode();
       current = Find(After);
       newNode.Next = current.Next;
       current.Next = newNode;
   }

    public void InsertAtFront(object Element)
    {
        if (IsEmpty())
        {
            first = last = new ListNode(Element);
        }
        else
        {
            first = new ListNode(Element,first);
        }
    }

    bool IsEmpty()
    {
        return first == null;
    }

    public void Display()
    {
        ListNode current = first;
        while (current!=null)
        {
            Console.WriteLine(current.Data);
            current = current.Next;
        }           
    }
}

I implement Find method for Add After specific element, but when I debug it showing me the object reference not set to an instance of an object exception. Please point out my mistake in Find method or with Add After method. thanks

  • 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-05-26T11:28:05+00:00Added an answer on May 26, 2026 at 11:28 am

    Problem is here

    while (current.Data != After)
          current = current.Next;
    

    When there is no After in your list you will eventually get current.Next equal to null
    You need to check if current.Next is not null

       while (current.Next != null && current.Data != After)
          current = current.Next;
    

    you should also fix your add logic (if you want to add elements into empty list)

       public void Add(object newItem, object After)
       {
           if(IsEmpty())
           {
                InsertAtFront(newItem);
                return;
           }
    
           ListNode newNode=new ListNode();
           newNode.Data = newItem; 
           ListNode current = Find(After);
           newNode.Next = current.Next;
           current.Next = newNode;
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class MyBase { protected object PropertyOfBase { get; set; } } class MyType :
Write a class ListNode which has the following properties: int value; ListNode *next; Provide
class A; class B { public: B(A& a) : a(a) {} private: A& a;
class Interface{}; class Foo: public Interface{}; class Bar{ public: vector<Interface*> getStuff(); private: vector<Foo*> stuff;
class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class
class C { T a; public: C(T a): a(a) {;} }; Is it legal?
#include<iostream> using namespace std; class TCSGraph{ public: void addVertex(int vertex); void display(); TCSGraph(){ head
class email is an array of email addresses. How do i get the array
So, for class I'm (constantly re-inventing the wheel) writing a bunch of standard data
I have a private static nested class in Java. What is the significance of

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.