Hi, i am working on a generic RingQueue ADT, however i came across a nullpointer exception during runtime and i cannot figure out what exactly my problem was.
Code:
/** Node of a singly linked list of Integer objects*/
public class Node<E>{
private E element; // assume elements are Integeregers
private Node<E> next;
/** Creates a Node with the given element and next node */
public Node(E element, Node n){
this.element = element;
next=n;
}
/** Returns the element of this node */
public E getElement(){return element;}
/** Returns the next node of this node */
public Node getNext(){return this.next;}
//Modified methods
/** Sets the element of this node */
public void setElement(E elem){System.out.println(elem); element = elem;}
/** Sets the next node of this node */
public void setNext(Node n){this.next=n;}
}
Code:
public class RingQueue<E> implements Queue<E>{
private Node<E> head,tail;
private int size, limit;
public RingQueue(int s){
head = tail = null;
limit = s; //max size of queue
size = 0; //num of elements in queue
setupQ(head,limit);
tail = head;
}
// other methods omitted ...
/**
* Insert an element at the rear of the queue
* @param element to be inserted
* @exception QueueFullException if queue is full
**/
public void enqueue (E element) throws QueueFullException{
if(isFull())
throw new QueueFullException("Queue already full");
else{
if(!isEmpty())
tail = tail.getNext();
tail.setElement(element); // this line always give me problem
size++;
}
}
above are my implementation of the RingQ adt, whenever the tail.setElement() method is invoked... or rather the setElement() of Node class is invoked, the program crashes.
Please advise