Could someone please give me an idea of why I get a nullPointerException when I run this method in my main program. Every list has a variable, first, to store the address of the first node of the list. I assign current1 to the first node of list1 as a local variable in my method, but when I run it I get a nullPointerException.
public void mergeLists (OrderedLinkedList<T> list1, OrderedLinkedList<T> list2)
{
LinkedListNode<T> current1, current2, current3;
current1 = list1.first; current2 = list2.first; current3 = new LinkedListNode();
while (current1 != null || current2 != null)
{
Comparable<T> temp1 = (Comparable<T>)current1.info;//why is current1.info null?
if (first == null)//If new list is empty set the first element
{
if (temp1.compareTo(current2.info) <= 0)
{
current3.info = current1.info;//copy the element in list1 to current3
first = current3;//first element in new list is first in list1
current1 = current1.link;//increment element in list1
last = first;//set the last variable for new list
count++;//increment the count for the new list
}
else if (temp1.compareTo(current2.info) > 0)
{
current3.info = current1.info;//copy the element in list2 to current3
first = current3;//first element in new list is first in list2
current2 = current2.link;//increment element in list2
last = first;//set the last variable for new list
count++;//increment the count for the new list
}
}
else//There is already an element in new list
{
if (temp1.compareTo(current2.info) <= 0)
{
current3.info = current1.info;//copy the element in list1 to current3
last.link = current3;//place current3 in new list
last = current3;//set the last variable in new list to current3
current1 = current1.link;//increment element in list1
count++;//increment the count for the new list
}
else if (temp1.compareTo(current2.info) > 0)
{
current3.info = current1.info;
last.link = current3;
last = current3;
current2 = current2.link;
count++;
}
}//end else
}//end while
}//end mergeLists
}//end LinkedListClass<T>