CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2010
    Posts
    6

    RingQueue ADT nullpointer exception

    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

  2. #2
    Join Date
    Oct 2010
    Posts
    6

    Re: RingQueue ADT nullpointer exception

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

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: RingQueue ADT nullpointer exception

    What would be helpful is the full stack trace from the null pointer exception. Also please highlight the line of code that the exception is thrown from.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Oct 2010
    Posts
    6

    Re: RingQueue ADT nullpointer exception

    thanks keang, i have already figured out what my problem was shortly after i posted it. Sometimes the best solution to a problem is to take a rest and fresh yourself before continueing on it, haha

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: RingQueue ADT nullpointer exception

    Sometimes the best solution to a problem is to take a rest and fresh yourself before continueing on it
    Yep, a fresh mind is always better at problem solving. Another way I have found useful is to explain to problem to someone who knows nothing about what you are trying to do (an imaginary idiot friend works pretty well). When doing this you have to go to levels of detail you often overlook in the heat of battle and part way through the explanation you'll suddenly realise where the problem is.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured