When calling my iterator over a doubly linked list, I am getting a null pointer exception. The null pointer exception occurs in main at line assertEquals(i, (int) it.next());
/***************************
* nested class DequeIterator
***************************/
private class DequeIterator implements Iterator<E>
{
// instance data member of ListIterator
private Node current;
// constructors for ListIterator
public DequeIterator()
{
current = first; // head in the enclosing list
}
public boolean hasNext()
{
return current != null;
}
public E next()
{
if (hasNext() == false){ throw new NoSuchElementException();}
else {
E ret = current.item;
current = current.next;
return ret;
}
}
public void addLast(E item) {
if (item.equals(null)) { throw new NullPointerException(); }
else {
Node node = new Node(item, null);
Node ptr = last;
ptr.prev.next = node;
node.next = ptr;
node.prev = ptr.prev;
ptr.prev = node;
N++;
}
}
public static void main(String[] args) {
Deque<Integer> lst = new Deque<Integer>(); // empty list
for(int i = 1; i <= 5; i++) {
lst.addLast(i);
}
assertEquals(5, lst.size());
Iterator<Integer> it = lst.iterator();
int i = 1;
while(it.hasNext()) {
assertEquals(i, (int) it.next());
i++;
}
assertEquals(6, i);
assertEquals(5, lst.size());
}
Can anybody tell me why I am getting a null pointer exception at that point?