Results 1 to 10 of 10
- 04-11-2010, 12:25 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
[Solved] Why would a ListIterator.add throw a NullPointerException?
I'm feeding it a string, but it always returns a null pointer.
is callingJava Code:BufferedReader buffin = new BufferedReader(new FileReader(fileName)); list.add(buffin.readLine());
iter is a ListIterator object.Java Code:public void add(E elem) { iter.add(elem);}
It seems a very simple process, but I've been hung up on it for over an hour now. I don't understand why it would be seeing a null object. I've checked the variable it's feeding, it's a single character.
Edit, I just tried the add method asking for a string instead, and then typecasting to an object, no bueno.Last edited by moriarty; 04-11-2010 at 07:39 PM. Reason: solved
-
Are you sure that the list itself isn't null? Or perhaps the BufferedReader? It's simple to check via some
or similar for the BufferedReader object.Java Code:System.out.println("is list null: " + (list == null));
- 04-11-2010, 12:38 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
Returns false. The list is empty, but shouldn't be null.
edit: ' is buffin null' returns false as well.Last edited by moriarty; 04-11-2010 at 12:41 AM.
-
Some variable must be holding a null object that you are trying to dereference, and this is causing a NPE. Your job is to find out which object it is. Is it the iterator? My advice is to keep testing til you find out what is null. Sorry I couldn't be more helpful.
You might want to show us more code, and wait to see if someone smart comes around here. Regardless, if you find the solution, please let us know.
Much luck.Last edited by Fubarable; 04-11-2010 at 12:54 AM.
- 04-11-2010, 12:55 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
:D I'll keep looking over it, I'm trying to post as little code as needed to get over the hump so I'm not getting TOO MUCH help.
- 04-11-2010, 07:24 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
Ok, so I'm losing my mind. This should work, and it's been a complete brick wall for over 12 hours for me. It's got to be something implicitly stupid on my part.
and the circular linked list is shown as followsJava Code:public class Josephus { public static Scanner keyboard; public static void main(String[] args) throws IOException { CircularLinkedList<String> list = new CircularLinkedList<String>(); keyboard= new Scanner(System.in); System.out.print("Enter file name: "); String fileName = keyboard.nextLine(); BufferedReader buffin = new BufferedReader(new FileReader(fileName)); list.add(buffin.readLine()); } }
What am I doing wrong? I've written this over and over, changed everything I can think of. I thought I had a good understanding of iterators. I'm simply trying to add at that iteration. The circular linked list is empty, but not null, and in the class description of list iterator, it states that if there is nothing in the list, it will add that as the first element.Java Code:public class CircularLinkedList<E> { private LinkedList<E> theList; private ListIterator<E> iter; public CircularLinkedList() { LinkedList<E> theList = new LinkedList<E>(); ListIterator<E> iter = theList.listIterator(); } public void add(E elem) { iter.add(elem); }Last edited by moriarty; 04-11-2010 at 07:29 PM.
-
It looks to be something that trips all of us one time or another. You're shadowing a class field in the constructor.
Look here:
So both the iterator and the list will be null.Java Code:public class CircularLinkedList<E> { private LinkedList<E> theList; // declared here private ListIterator<E> iter; // declared here. public CircularLinkedList() { LinkedList<E> theList = new LinkedList<E>(); // redeclared and discarded @SuppressWarnings("unused") ListIterator<E> iter = theList.listIterator(); // iterator redeclared and discarded }
Solution: Initialize but don't re-declare the LinkedList inside the constructor.Last edited by Fubarable; 04-11-2010 at 07:31 PM.
- 04-11-2010, 07:31 PM #8
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
Ahhh, I was just going over that and was thinking the same thing, it was pre-defined code given to us, so I was doing my best to keep that unedited. I'll try it out, thanks.
- 04-11-2010, 07:36 PM #9
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
Ahhh, thank the heavens. I think that did it, I can finally continue. Thank you so much.
For anyone wanting to learn from my mistake, the correct code is now:
Java Code:private LinkedList<E> theList; private ListIterator<E> iter; public CircularLinkedList() { theList = new LinkedList<E>(); iter = theList.listIterator(); }
-
You're welcome. Good luck with the project.
Similar Threads
-
ListIterator implementation
By hari.kr in forum Advanced JavaReplies: 2Last Post: 02-15-2010, 09:14 AM -
what exception to throw
By DoolinDalton in forum New To JavaReplies: 5Last Post: 02-10-2010, 03:45 PM -
throw exception
By GIRISH PATEL in forum New To JavaReplies: 4Last Post: 04-23-2009, 04:35 AM -
Bidirectional Traversal with ListIterator
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:37 PM -
using ListIterator with ArrayList
By Java Tip in forum Java TipReplies: 0Last Post: 11-13-2007, 10:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks