My program compiles with no errors but when I run the program it asks "Enter the number to be deleted:" if I enter any number other than the first number listed the program crashes with the following error:

"An unhandled exception of type 'System.NullReferenceException' occurred in doublyLinkedList.exe

Additional information: Object reference not set to an instance of an object."

#ifndef H_doublyLinkedList
#define H_doublyLinkedList

#include <iostream>
#include <cassert>

using namespace std;

template <class Type>
struct  nodeType
{  
      Type info;
	  nodeType<Type> *next;
      nodeType<Type> *back;  
};

template <class Type>
class doublyLinkedList
{
    friend ostream& operator<<(ostream&, 
                           const doublyLinkedList<Type>&);
     
public:
    const doublyLinkedList<Type>& operator=
                           (const doublyLinkedList<Type> &);
    void initializeList();
    bool isEmptyList();
    void destroy();
	void printList() const;
    void reversePrint();
    int length();
    Type front();
    Type back();
    bool search(const Type& searchItem);
    void insertNode(const Type& insertItem);
    void deleteNode(const Type& deleteItem);
    doublyLinkedList(); 
    doublyLinkedList(const doublyLinkedList<Type>& otherList); 
    ~doublyLinkedList(); 
    
protected:
    int count;
	nodeType<Type> *first; 
    nodeType<Type> *last; 

private:
    void copyList(const doublyLinkedList<Type>& otherList); 
 };


template<class Type>
doublyLinkedList<Type>::doublyLinkedList()
{
	first= NULL;
	last = NULL;
	count = 0;
}

template<class Type>
bool doublyLinkedList<Type>::isEmptyList()
{
    return(first == NULL);
}

template<class Type>
void doublyLinkedList<Type>::destroy()
{ 
	nodeType<Type>  *temp; 
	
	while(first != NULL)
	{
		temp = first;
		first = first->next;
		delete temp;
	}

	last = NULL;
	count = 0;
}

template<class Type>
void doublyLinkedList<Type>::initializeList()
{
	destroy();
}

template<class Type>
int doublyLinkedList<Type>::length()
{
	return count;
}

template<class Type>
ostream& operator<<(ostream& osObject, 
					const doublyLinkedList<Type>& list)
{
    nodeType<Type> *current; 

	current = list.first;  

	while(current != NULL)
	{
	   cout<<current->info<<"  ";  
	   current = current->next;
	}

	return osObject;
}

template<class Type>
void doublyLinkedList<Type>::printList() const
{
    nodeType<Type> *current;

	current = first;

	while (current != NULL)
	{
	   cout << current->info << "  ";
	   current = current->next;
	}
}

template<class Type>
void doublyLinkedList<Type>::reversePrint()
{
      nodeType<Type> *current; 

	  current = last;  

      while(current != NULL)
      {
	      cout<<current->info<<"  ";  
	      current = current->back;
      }
}

template<class Type>
bool doublyLinkedList<Type>::search(const Type& searchItem)
{
    bool found;
    nodeType<Type> *current; 

    found = false;
    current = first;

    while(current != NULL && !found)
        if(current->info >= searchItem)
           found = true;
        else
           current = current->next;

    if(found)
       found = (current->info == searchItem); 

    return found;
}

template<class Type>
Type doublyLinkedList<Type>::front()
{
      assert(first != NULL);

	      return first->info;
}

template<class Type>
Type doublyLinkedList<Type>::back()
{
      assert(last != NULL);

      return last->info;
}

template<class Type>
void doublyLinkedList<Type>::insertNode(const Type& insertItem)
{
    nodeType<Type> *current; 
    nodeType<Type> *trailCurrent;
    nodeType<Type> *newNode;  
    bool found;

    newNode = new nodeType<Type>; 
    assert(newNode != NULL);

    newNode->info = insertItem; 
    newNode->next = NULL;
    newNode->back = NULL;

    if(first == NULL) 
    {
       first = newNode;
       last = newNode;
       count++;
    }
    else
    {
        found = false;
        current = first;

        while(current != NULL && !found)
            if(current->info >= insertItem)
               found = true;
            else
            {
               trailCurrent = current;
               current = current->next;
            }

       if(current == first)
       {
          first->back = newNode;
          newNode->next = first;
          first = newNode;
          count++;
       }
       else
       {
          if(current != NULL)
          {
             trailCurrent->next = newNode;
             newNode->back = trailCurrent;
             newNode->next = current;
             current->back = newNode;
          }
          else
          {
             trailCurrent->next = newNode;
             newNode->back = trailCurrent;
             last = newNode;
          }
          count++;
       }
    }
}

template<class Type>
void doublyLinkedList<Type>::deleteNode(const Type& deleteItem)
{
   nodeType<Type> *current;
   nodeType<Type> *trailCurrent;

   bool found;

   if(first == NULL)
      cerr<<"Cannot delete from an empty list"<<endl;
   else
      if(first->info == deleteItem)
      {
         current = first;
         first = first->next;

         if(first != NULL)
            first->back = NULL;
         else
            last = NULL;
			
         count--;
         delete current;
      }
      else 
      {
         found = false;
          current = first;

         while(current != NULL && !found) 
            if(current->info >= deleteItem)
               found = true;
            else
               current = current->next;

         if(current == NULL)
           cout<<"The item to be deleted is not in the list"
		       <<endl;
        else
           if(current->info == deleteItem)
           {
              trailCurrent = current->back; 
              trailCurrent->next = current->next;

              if(current->next != NULL)
                 current->next->back = trailCurrent;

              if(current == last)
                 last = trailCurrent;

              count--;
              delete current;
          }
          else
             cout<<"The item to be deleted is not in list."
			     <<endl;
       }
}

template<class Type>
void doublyLinkedList<Type>::copyList(const doublyLinkedList<Type>& otherList)
{
	nodeType<Type> *newNode;
	nodeType<Type> *current;

   if(first != NULL)
	  destroy();

   if(otherList.first == NULL)
   {
		first = NULL;
		last = NULL;
 		count = 0;
   }
   else
   {
		current = otherList.first; 
									
		count = otherList.count;

			
		first = new nodeType<Type>; 

 		assert(first != NULL);

		first->info = current->info;
		first->next = NULL; 
		
		last = first; 
        
		current = current->next;   
        
		while(current != NULL)
		{
			newNode = new nodeType<Type>;

			assert(newNode!= NULL);

			newNode->info = current->info;
			newNode->next = NULL;  
           
			last->next = newNode;
			last = newNode; 
			
			current = current->next;
		}
	} 
}

template<class Type>
doublyLinkedList<Type>::doublyLinkedList(const doublyLinkedList<Type>& otherlist)
{
	  first = NULL;

	copyList(otherList); 
}

template<class Type>
const doublyLinkedList<Type>& doublyLinkedList<Type>::operator=
							(const doublyLinkedList<Type> & otherList)
{
	if(this != &otherList) 
		copyList(otherList);

	return *this; 
}

template<class Type>
doublyLinkedList<Type>::~doublyLinkedList()
{
	 destroy(); 
}

#endif

#include <iostream>
#include "doublyLinkedList.h"

using namespace std; 

int main()
{
    doublyLinkedList<int> list1, list2;	
	int num;

	cout << "Enter numbers ending with -999" << endl;	
	cin >> num;

	while(num != -999)
	{
		list1.insertNode(num);
		cin >> num;
	}
	cout << endl;	

	cout << "List 1: ";
	list1.printList();
	cout << endl;	
	cout << "Length List 1: " << list1.length() << endl;	

	list2 = list1;

	cout << "List 2: ";
	list2.printList();
	cout << endl;	
	cout << "Length List 2: " << list2.length() << endl;

	cout << "Enter the number to be deleted: ";
	cin >> num;
	list2.deleteNode(num);
	cout << endl;
	
	cout << "After deleting the node, List 2: ";
	list2.printList();
	cout << endl;
	cout << "Length List 2: " << list2.length() << endl;

	return 0;					
}

any ideas as to what is causing this error?

learn to use your compiler's debugger and single-step through the code so that you can see what is going on. It's better than just guessing.

Such big code does not help.
Pinpoint the place where you get the error message.

You probably missed initialisation of an object so that it is null.
If you call the object members, then null reference exception will occur..

Regards,
:)
Paramesh.

Focus your attention here in deleteNode :

if(current->info == deleteItem)
           {
              trailCurrent = current->back; 
              trailCurrent->next = current->next;

              if(current->next != NULL)
                 current->next->back = trailCurrent;

              if(current == last)
                 last = trailCurrent;

              count--;
              delete current;
          }

Does this function look ok?

template<class Type>
void doublyLinkedList<Type>::copyList(const doublyLinkedList<Type>& otherList)
{
	nodeType<Type> *newNode;
	nodeType<Type> *current;

   if(first != NULL)
	  destroy();

   if(otherList.first == NULL)
   {
		first = NULL;
		last = NULL;
 		count = 0;
   }
   else
   {
		current = otherList.first; 
									
		count = otherList.count;

			
		first = new nodeType<Type>; 

 		assert(first != NULL);

		first->info = current->info;
		first->next = NULL; 
		
		last = first; 
        
		current = current->next;   
        
		while(current != NULL)
		{
			newNode = new nodeType<Type>;

			assert(newNode!= NULL);

			newNode->info = current->info;
			newNode->next = NULL;  
           
			last->next = newNode;
			last = newNode; 
			
			current = current->next;
		}
	} 
}
This article has been dead for over six months. Start a new discussion instead.