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>

Recommended Answers

All 3 Replies

NPE at which line? The message tells you that. Then look at that line to see which variable(s) could be null. Print it/them to confirm, then work backwards to find out why.
(I'm finishing now for this evening, someone else will probably step in...)

It gives me the error for line 8, when I compare, but I think I might know the problem. The abstract class LinkedListClass<T> has two inner classes: LinkedListNode<T> and LinkedListIterator<T>. The variables current1 and current2, above in mergeLists(list1, list2), should be objects of the class Iterator, not Node, but I don't understand how to initialize them. The Iterator class has two variables of the Node class: current and previous, but I don't understand how to call the constructor, LinkedListIterator(), on list1, to initialize the object in my method above.

What variable has the null value? Is that variable assigned a value before it is used?

If you want any help debugging the problem, you will need to post a small, complete program that compiles, executes and shows the problem.

while (current1 != null || current2 != null)

This is true if either one of those variables is NOT null. If you want it to be true when both are not null, use an AND operator instead of OR.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.