Java 泛型LinkedQueue,无法从内部迭代器类访问head变量

Java 泛型LinkedQueue,无法从内部迭代器类访问head变量,java,generics,nullpointerexception,tail,head,Java,Generics,Nullpointerexception,Tail,Head,我已经为类制作了一个通用链接队列的自己的实现,基本完成了,如下所示: public class LinkedQueue<T> implements Queue<T> { //Using head & tail approach. private myNode<T> head; private myNode<T> tail; private int size; public LinkedQueue(){ this.head = n

我已经为类制作了一个通用链接队列的自己的实现,基本完成了,如下所示:

public class LinkedQueue<T> implements Queue<T> {

//Using head & tail approach.
private myNode<T> head;
private myNode<T> tail;
private int size;

public LinkedQueue(){
    this.head = null;
    this.tail = head;
    this.size = 0;
}

public myNode<T> getterHead(){ //couldn't bring myself to write "get" instead of "getter"
    return this.head;
}

@Override
public int size() {
    return this.size; //returns number of nodes
}

@Override
public boolean isEmpty() {
    return this.head==null;
}

@Override
public void enqueue(T element) {
    if(isEmpty()){
        this.head = new myNode<T>(element);
        this.tail = head;
        size++;
    }
    else{
        this.tail.next = new myNode<T>(element);
        this.tail = tail.next;
        size++;
    }

}

@Override
public T dequeue() {
    if(isEmpty())throw new NoSuchElementException("This queue is empty");

    T returnObj = this.head.data; //saving the data of the first element(head)

    //If there are at least 2 nodes, else.
    if(head != tail){
        this.head = head.getNext(); 
        size--;

    }
    else{
        this.head = null;
        this.tail = head;
        this.size = 0;
    }
    return returnObj;
}

@Override
public T first() {
    return this.head.data;
}

@Override
public T last() {
    return this.tail.data;
}

/* I absolutely can not get past this NullPointerException that is given every time
 * I try to use the iterator. It seems that current.next is always null(doesn't point to head properly?).
 * HOWEVER, if you change "curr" for "head", it works. */ 
@Override
public Iterator<T> iterator() {
    return new GenericIterator();
}

private class GenericIterator implements Iterator<T>{

    public myNode<T> curr = head;

    public boolean hasNext() {
        return (head != null && head.next != null);
    }

    public T next() {
            T tmp = head.data; //saving current in a temporary node because we are changing the value of current in the next line
            if(hasNext()){
                head = head.next;
            }
            return tmp;
    }
}


private class myNode<T> { //parameter type T hiding type T

    public T data;  //The generic data the nodes contain
    public myNode<T> next;  //Next node

    //Node constructor
    public myNode(T nData) {
        this.data = nData;
        this.next = null;
    }

    public myNode<T> getNext(){
        return this.next;
    }
}
}
其中第101行和第17行为:

T tmp = curr.data; //node property .data & .next are null.
System.out.println(it.next()); //LinkedQueueMain.java(not included in post), both it.next() and it.hasNext() produce this NPE.

同样,问题的标题与您在第二个代码块中描述的问题之间存在很大的差异。请更改其中一个以匹配另一个。@JoeC我已经完成了整个线程,正如我提到的,我从几个小时前就开始尝试修复这个问题。我访问了与此最相关的话题。我的stacktrace只有一行,调试表明curr应该从myNode head继承的所有属性都为null。在这种情况下,您似乎需要学习使用调试器。请随便吃点。如果您以后仍然有问题,请随时回来用一个演示您的问题的文件。用完整的堆栈跟踪发布您的NPE。
Exception in thread "main" java.lang.NullPointerException
at dk222gw_lab4.Queue.LinkedQueue$GenericIterator.next(LinkedQueue.java:101)
at dk222gw_lab4.Queue.LinkedQueueMain.main(LinkedQueueMain.java:17)
T tmp = curr.data; //node property .data & .next are null.
System.out.println(it.next()); //LinkedQueueMain.java(not included in post), both it.next() and it.hasNext() produce this NPE.