0

this is for an assignment. I need to implement a queue using linked lists. I keep getting a null pointer exception while using my enqueue method for my listQueue Class. I'm not too sure what is going on. Below are my main class and listQueue Class.

public class Driver{

    public static void main(String[] args) {

        MyQueue<Integer> q = null;

        boolean useList = true;  // make it false if you want to array implementation

        if (useList)
            q = new ListQueue<Integer>();   // default setting: use a list to implement the queue. You can change it.
        else
            q = new ArrayQueue<Integer>();

        for(int i = 0; i < 1000; i++)  // add a large number of items onto the queue
        {
            q.enqueue(i);  
        }

        System.out.println("Now, dequeue items!");
        while(!q.isEmpty())  
        {
            System.out.print(q.dequeue() + " ");  
        }
        System.out.println("\nEnd of dequeueing");    

        // you should fully test/debug all methods!

    }

}


public class ListQueue<T> implements MyQueue<T> {

    private class Node {

        T item; // public
        Node next;  // null

    }

    private Node head = null;
    private Node tail = null;
    private int size = 0; //size of the list

    public ListQueue(){}

    /**
     * is empty?
     */
    public boolean isEmpty() {
        // your implementation here:
        if (size == 0) {
            return true;
        }//end if
        return false;
    }//end isEmpty

    /**
     * @return the number of items in the queue
     */
    public int size() {
        // your implementation here:
        return size;
    }//end size

    /**
     * enqueue: add an element at the tail of queue
     */
    public void enqueue(T item) {
        // your implementation here:
        if (size == 0) {
            head = new Node();
            head.item = item;
            head.next=tail;
            size++;
        }//end if
        else{
            Node newNode = new Node();
            newNode.item=item;
            tail.next = newNode;
            tail = newNode;
            size++;
        }//end else
    }

    /**
     * dequeue: remove and return the head of the queue
     *
     * @return the deleted value
     * @throws NoSuchElementException if queue is empty
     */
    public T dequeue() {
        // your implementation here:
        if(isEmpty()){
            throw new NoSuchElementException();
        }//end if
        else{
            Node temp = head;
            head = head.next;
            size--;
            return temp.item;
        }//end else
    }//end dequeue

    /**
     * peek: view the head of queue without removing it.
     *
     * @return Null if queue is empty
     */
    public T peek() {
        // your implementation here:
        if(isEmpty()){
        return null;
        }//end if
        else{
            return head.item;
        }//end else
    }//end peek
}//end ListQueue
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Natedog
  • 13
  • 2

1 Answers1

0

After adding one element to the list, your tail pointer is null. When you add another element to the list, you try to dereference the null pointer which causes the error:

tail.next = newNode;

You need to make sure that for non-empty queues, the head and tail pointers are always pointing to a node. When the queue is size 1, the head and tail pointers should point to the same node.

tmajest
  • 360
  • 1
  • 5