Object reference not set to an instance of an object in C#
<p>this is my code for linked list in C# and when i run it, it gives correct output , but program crashes after some seconds and generates an exception "Object reference not set to an instance of an object."
please help me out..</p>

<pre><code>class Node
{
    public int info;
    public Node next;

    public Node(int a)
    {
        info = a;
    }

    public void DisplayNode()
    {
        Console.WriteLine(info);
    }
}

class MyList
{
    private Node first;

    public MyList()
    {
        first = null;
    }

    public void InsertFirst(int data)
    {
        Node newNode= new Node(data);
        if (first != null)
        {
            newNode.next = first;
            first = newNode;
        }
        else
        {
            first = newNode;
            newNode.next = null;
        }
    }

    public Node DeleteFirst()
    {
        Node ptr = first;
        first = first.next;
        return ptr;
    }

    public void displayList()
    {
        Console.WriteLine("List (First --&gt; Last): ");
        Node current = first;
        while (current != null)
        {
            current.DisplayNode();
            current = current.next;
        }
        Console.WriteLine(" ");
    }
}
</code></pre>

<p>class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MyList newList = new MyList();</p>

<pre><code>            newList.InsertFirst(10);
            newList.InsertFirst(20);
            newList.InsertFirst(30);
            newList.InsertFirst(40);

            newList.displayList();

            while (newList != null)
            {
                Node aNode = newList.DeleteFirst();
                Console.WriteLine("Deleted");
                aNode.DisplayNode();
                Console.WriteLine(" ");
            }

            newList.displayList();
        }

        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
        }

        Console.ReadLine();
    }
</code></pre>
