Results 1 to 10 of 10
  1. #1
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default [Solved] Why would a ListIterator.add throw a NullPointerException?

    I'm feeding it a string, but it always returns a null pointer.

    Java Code:
     BufferedReader buffin = new BufferedReader(new FileReader(fileName));
     list.add(buffin.readLine());
    is calling
    Java Code:
     public void add(E elem) {
     iter.add(elem);}
    iter is a ListIterator object.

    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you sure that the list itself isn't null? Or perhaps the BufferedReader? It's simple to check via some
    Java Code:
    System.out.println("is list null: " + (list == null));
    or similar for the BufferedReader object.

  3. #3
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by moriarty View Post
    Returns false. The list is empty, but shouldn't be null.

    edit: ' is buffin null' returns false as well.
    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.

  5. #5
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    :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.

  6. #6
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    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.

    Java 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());
    
    	}
    
    }
    and the circular linked list is shown as follows

    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);
    	}
    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.
    Last edited by moriarty; 04-11-2010 at 07:29 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by moriarty View Post
    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.
    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:
    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
    	}
    So both the iterator and the list will be null.

    Solution: Initialize but don't re-declare the LinkedList inside the constructor.
    Last edited by Fubarable; 04-11-2010 at 07:31 PM.

  8. #8
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    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.

  9. #9
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

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

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. ListIterator implementation
    By hari.kr in forum Advanced Java
    Replies: 2
    Last Post: 02-15-2010, 09:14 AM
  2. what exception to throw
    By DoolinDalton in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 03:45 PM
  3. throw exception
    By GIRISH PATEL in forum New To Java
    Replies: 4
    Last Post: 04-23-2009, 04:35 AM
  4. Bidirectional Traversal with ListIterator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:37 PM
  5. using ListIterator with ArrayList
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-13-2007, 10:18 AM

Posting Permissions

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