I've been struggling to get rid of a NullPointerException that occurs when calling the line of code:
if (priorityComparator.compare(temp.next.value, newNode.value) >= 0 )
The full code is:
public class HeaderLinkedPriorityQueue<E> extends
AbstractPriorityQueue<E> implements PriorityQueue<E> {
//Some other methods, constructors etc.
public boolean add (E e) {
ListNode<E> temp = highest;
ListNode<E> newNode = new ListNode<E>(e, null);
if (temp.next == null){
//first node in a list.
temp.next = newNode;
objectCount++;
return true;
}
//if the value of the first element following the header node is greater than the newNode add to back.
if (priorityComparator.compare(temp.next.value, newNode.value) >= 0 ) {
temp.next.next = newNode;
objectCount++;
}
else {
//add before the first node in the list. have temp.next point to newNode and have newNode point to the old temp.next.
newNode.next = temp.next;
temp.next = newNode;
objectCount++;
}
return true;
}
//class variables.
private ListNode<E> highest = new ListNode(null, null);
private int objectCount = 0;
private Comparator<? super E> priorityComparator;
I don't see anything wrong with the parameters so I'm really stumped. How can I fix this?
It seems like you didn't initialize your PriorityComparator.
private Comparator<? super E> priorityComparator;
should be something like
private Comparator<? super E> priorityComparator = new PriorityComparator();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.