Results 1 to 8 of 8
Thread: java.lang.NullPointerException
- 09-18-2009, 11:05 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
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:
you will probably need this code too, to help me figure out the problem: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."); } }
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.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("!"); } }
- 09-18-2009, 11:09 AM #2
Hi.
_contents is just a reference know.U have not created an object.Might be it is throwing nullpointer exception.Ramya:cool:
- 09-18-2009, 11:12 AM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Read this article.
- 09-18-2009, 11:58 AM #4
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
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?
- 09-18-2009, 12:04 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
If you can't read that article and apply it to your problem then you can't solve your problem at all.
- 09-18-2009, 12:58 PM #6
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:
- 09-18-2009, 05:42 PM #7
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
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?
- 09-18-2009, 05:49 PM #8
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Similar Threads
-
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 3Last Post: 02-28-2009, 05:41 AM -
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 1Last Post: 02-27-2009, 12:36 PM -
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 2Last Post: 02-27-2009, 10:11 AM -
java.lang.NullPointerException
By stevemcc in forum AWT / SwingReplies: 2Last Post: 02-08-2008, 09:01 AM -
java.lang.NullPointerException
By Felissa in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 06:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks