Why am I getting a null pointer exception in my iterator?

When I call my iterator over a doubly linked list, I get a null pointer exception. Null pointer exception occurs on the main lineassertEquals(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 someone tell me why I am getting a null pointer exception at this point?

+4
source share
3 answers

Start by considering the conditions for ending the loop:

public boolean hasNext(){
    return current != null;
}

This means that the last time it is executed, it will return null, since it checks that the current element, and not the next element, is non-zero.

So your code will become something like this:

Integer it_next = null;
assertEquals(i, (int)it_next);

int - , . unboxing, , :

r Integer, unboxing r r.intValue()

,

((Integer)null).intValue();

, .

, , , - true hasNext, next null. - :

public boolean hasNext(){
    return current != null && current.next != null;
}
+1

Integer , null Integer, int ...

, , null:

while(it.hasNext()) {
          Integer e = it.next();
          if(e != null)
            assertEquals(i, (int)e );
          i++;
        }
+2

Given the incomplete code that you sent, it seems that there is a deque end token with a name lastand that it itemis most likely null.

Correct your iterator to check lastin hasNext():

public boolean hasNext()
{
    return current != last && current != null;
}

The check for has nullbeen saved for security. This is probably not necessary.

+1
source

Source: https://habr.com/ru/post/1613846/


All Articles