• October 6th, 2010, 09:56 PM
J-Ideas
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.

• October 6th, 2010, 10:01 PM
J-Ideas
I am not sure if this will be useful... This is the method i used to set up my ringQueue. and it also belongs to the RingQueue class.

Code:

```public class RingQueue{         ...         ...         /**         * Set up a circular linked list / Queue of length L         * @param reference to the front of the queue         * @param max length of the queue         **/         private void setupQ(Node head,int L){                 Node temp = head;                 for(int i=0;i<L;i++){                         if(temp == null)                                 temp = new Node(null,null);                         else if(i+1==L)                                 temp.setNext(head);                         else{                                 temp.setNext(new Node(null,null));                                 temp=temp.getNext();                         }                 }                 System.out.println("Ring Queue setup complete");         } }```
• October 7th, 2010, 03:17 AM
keang