0

I have made my own implementation of a generic Linked Queue for class, it is pretty much finished, here it is:

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;
    }
}
}

Here is the part that is giving me trouble:

/* 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; //doesn't work with getter either.
    //public myNode<T> curr = getterHead();

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

    public T next() {
            T tmp = curr.data; //NPE HERE!
            if(hasNext()){
                curr = curr.next;
            }
            return tmp;
    }
}

What I have tried is getters/setters and triple-checking every other method(they work). What seems to be the problem is that when I assign curr = head, it seems that the properties from myNode do not come with.

While head.next works fine, curr.next == null, even curr.data == null while head.data works.

I've tried with public properties and printing LinkedQueue.head.next etc, it works fine.

Stacktrace:

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)

Where line 101 & 17 are:

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.
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Joe C Mar 18 '17 at 22:40
  • Also, there is a big difference between the title of your question and the problem you describe in your second code block. Please change one to match the other. – Joe C Mar 18 '17 at 22:42
  • @JoeC I've gone through the whole thread, as I mentioned I'm trying to fix this since hours back. I have visited the most related topics on SO. My stacktrace is only 1 line, and debugging has shown that all the properties that curr is supposed to inherit from myNode head, are all null. –  Mar 18 '17 at 23:02
  • In that case, it looks like you need to learn to use a debugger. Please help yourself to some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you still have issues afterwards, please feel free to come back with a [_Minimal_, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) that demonstrates your problem. – Joe C Mar 18 '17 at 23:04
  • Post your NPE with full stack trace. – PM 77-1 Mar 18 '17 at 23:26
  • @PM77-1 Edited OP. –  Mar 18 '17 at 23:32

0 Answers0