Results 1 to 8 of 8
  1. #1
    Travis is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default java.lang.NullPointerException

    Hey,
    I've been having a problem with one of my programming assignments.
    I keep getting the message:

    Exception in thread "main" java.lang.NullPointerException
    at queueTask.TpQueue.front(TpQueue.java:29)
    at queueTask.TpQueue.main(TpQueue.java:53)

    on this code:

    Java Code:
    package queueTask;
    
    import simpleLists.*;
    
    public class TpQueue<E> implements Queue<E> {
    	
    	private SimpleList<E> _contents;
    	
    
    	@Override
    	public E deQueue() throws EmptyQueueException {
    		// TODO Auto-generated method stub
    		if (_contents.isEmpty() == true)
    			throw new EmptyQueueException("Queue is empty.");
    		E tmp =_contents.removeFirst();
    		return tmp;
    	}
    
    	@Override
    	public void enQueue(E item) {
    		// TODO Auto-generated method stub
    		Node<E> node = new Node<E>();
    		node.setElement(item);
    		node.setNext(null);
    		}
    	@Override
    	public E front() throws EmptyQueueException {
    		// TODO Auto-generated method stub
    		if (_contents.isEmpty()== true)
    			throw new EmptyQueueException("Queue is empty");
    		else
    			return _contents.firstElement();
    	}
    
    	@Override
    	public boolean isEmpty() {
    		// TODO Auto-generated method stub
    		if( _contents.isEmpty()== true)
    			return true;
    		else
    			return false;
    	}
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// some limited testing
    		TpQueue<String> testCase = new TpQueue<String>();
    		for (int i=0;i<10;i++)
    			testCase.enQueue("Robert");
    		
    		System.out.println("First = " + testCase.front());
    		
    		for (int i = 0; i<10; i++) 
    			testCase.deQueue();
    		
    		if (testCase.isEmpty())
    			System.out.println("It WORKS! YIPPIE!");
    		else 
    			System.out.println("It does not work.  Darn.");
    	
    
    	}
    
    }
    you will probably need this code too, to help me figure out the problem:

    Java Code:
    /**
     * 
     */
    package simpleLists;
    
    /**
     * @author robert
     *
     */
    public class TpList<E> implements SimpleList<E> {
    	
    	// instance variables: the first and last Node in the list
    	private Node<E> _front;
    	private Node<E> _rear;
    	
    	/* (non-Javadoc)
    	 * @see simpleLists.SimpleList#addAtFront(java.lang.Object)
    	 */
    	@Override
    	public void addAtFront(E item) {
    		// TODO Auto-generated method stub
    		_front = new Node<E>(item, _front);
    	}
    
    	/* (non-Javadoc)
    	 * @see simpleLists.SimpleList#addAtRear(java.lang.Object)
    	 */
    	@Override
    	public void addAtRear(E item) {
    		// TODO Auto-generated method stub
    		if (_front != null){
    			_rear = _front;
    			while (_rear.getNext() != null)
    				_rear = _rear.getNext();
    			_rear.setNext(new Node<E>(item,null));}
    		else _front = _rear = new Node<E>(item,null);
    		}
    
    	/* (non-Javadoc)
    	 * @see simpleLists.SimpleList#firstElement()
    	 */
    	@Override
    	public E firstElement() throws EmptyListException {
    		// TODO Auto-generated method stub
    		if (_front == null){
    			throw new EmptyListException ("This list is empty.");
    		}
    		else {
    			E v = _front.getElement();
    			return v; 
    		}
    	}
    
    	/* (non-Javadoc)
    	 * @see simpleLists.SimpleList#isEmpty()
    	 */
    	@Override
    	public boolean isEmpty() {
    		// TODO Auto-generated method stub
    		if (_front == null)
    			return true;
    		else
    			return false;
    	}
    
    	/* (non-Javadoc)
    	 * @see simpleLists.SimpleList#removeFirst()
    	 */
    	@Override
    	public E removeFirst() throws EmptyListException {
    		// TODO Auto-generated method stub
    		if (_front == null)
    			throw new EmptyListException("The list is empty.");
    		E t = _front.getElement();
    		_front = _front.getNext();
    		return t;
    
    	}
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		// This may be useful for simple testing
    		TpList<String> lister = new TpList<String>();
    		lister.addAtRear("my");
    		lister.addAtRear("hero");
    		lister.addAtFront("is");
    		lister.addAtFront("Robert");
    		while (!lister.isEmpty()) 
    			System.out.print(lister.removeFirst() + " " );
    		System.out.println("!");			
    	}
    }
    Please if you can help at all, i'd really appreciate it. I've been up all night trying to figure it out, and I'm super tired now xD.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    808
    Rep Power
    6

    Default

    Hi.
    _contents is just a reference know.U have not created an object.Might be it is throwing nullpointer exception.
    Ramya:cool:

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Read this article.

  4. #4
    Travis is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the replies, but i still wasn't able to fix it, mainly because there was so much the link, that i didn't know what to actually look for. But is there anything wrong with those specific lines of code that i should fix?

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    If you can't read that article and apply it to your problem then you can't solve your problem at all.

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    808
    Rep Power
    6

    Default

    Hi,
    ro35198x has given explanation in the below link.Take some time and gothru.

    But,as per the code posted and line numbers,the below reference variable is not pointing to any object.Please check ur code fully and put proper debug stattements as S.O.Ps.

    private SimpleList<E> _contents;
    Ramya:cool:

  7. #7
    Travis is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    thanks ramya, i instantiated it and i got rid of the error. However, now its giving me

    Exception in thread "main" queueTask.EmptyQueueException: Queue is empty.
    at queueTask.TpQueue.front(TpQueue.java:29)
    at queueTask.TpQueue.main(TpQueue.java:53)

    for those same pieces of code. This one i can't even find on the internet. Any ideas of whats causing it?

  8. #8
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Look at your TpQueue class at the line specified(29) and you'll notice you're throwing it.

    With that information you should be able to backtrack why it is being thrown.

Similar Threads

  1. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 05:41 AM
  2. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:36 PM
  3. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 2
    Last Post: 02-27-2009, 10:11 AM
  4. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 AM

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
  •