Results 1 to 9 of 9
  1. #1
    cpguy is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default LinkedList Iterator

    For an assignment, I had to create my own LinkedList class -- called BasicLinkedList -- that has many methods similar to LinkedList. I have to create a BasicListIterator that can iterate through the list, which contains the next(), previous(), hasNext(), and hasPrevious() methods. I'm having trouble with this, and keep getting NullPointerExceptions whenever I try to test it. Please let me know what I'm doing wrong!!
    Java Code:
    private class Node
       {
          public E data;
          public Node next;
          public Node prev;
    
       }
       private Node head;
    
    
    private class MyListIterator implements BasicListIterator<E>
       {
          private Node position = head;
          
          public E next()
          {
             if(!(hasNext()))
             {
                throw new NoSuchElementException();
             }
             else
             {
                E data = position.data;
                position = position.next;
                return data;
             }
          }
          
          public E previous()
          {
             if(!(hasPrevious()))
             {
                throw new NoSuchElementException();
             }
             else
             {
                System.out.println(position);
                E data = position.prev.data;
                position = position.prev;
                return data;
             }
          }
          
          public boolean hasNext()
          {
             if(position == null)
             {
                return false;
             }
             else
             {
                return true;
             } 
          }
          
          public boolean hasPrevious()
          {
             if(position == head)
             {
                return false;
             }
             else
             {
                return true;
             }
          }
          
          
          
          public void remove()
          {
             throw new UnsupportedOperationException();
          }
       }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: LinkedList Iterator

    We don't read minds. Post your errors and indicate on which lines they occur.

  3. #3
    cpguy is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: LinkedList Iterator

    Sorry. Here are my tests. When I call iter.previous(), I get this error:
    Exception in thread "main" java.lang.NullPointerException
    at BasicLinkedList$MyListIterator.previous(BasicLinke dList.java:38)

    Java Code:
    BasicLinkedList<Integer> list = new BasicLinkedList<Integer>();
    int[] ints = new int[] {99, 88, 77, 66, 55};
    list.add(ints[0]);
    BasicListIterator<Integer> iter = list.basicListIterator();
    
    iter.next() == ints[0];
    iter.previous() == ints[0];
    Last edited by cpguy; 11-17-2011 at 03:47 AM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: LinkedList Iterator

    Java Code:
    E data = position.prev.data;
    Is that line 38?
    When position is head then prev is null. So prev.data will throw a NPE.

  5. #5
    cpguy is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: LinkedList Iterator

    I changed it to just "position.data" but am still getting the same error...

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: LinkedList Iterator

    Think about it!

    When you are at the head does it have a previous?

  7. #7
    cpguy is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: LinkedList Iterator

    No -- but we aren't at head. After calling iter.next(), we're at the next node; so prev would be the node we just went over.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: LinkedList Iterator

    I see you have a print statement. Change it to print previous.data to see exactly which node is throwing the NPE. Then trace back as to what and why it is null.

  9. #9
    cpguy is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: LinkedList Iterator

    Tried that... Still can't figure it out. :(

Similar Threads

  1. Iterator
    By Dayanand in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 12:17 PM
  2. How to use linkedlist 'add' method without iterator
    By plexus0208 in forum New To Java
    Replies: 1
    Last Post: 11-25-2010, 08:27 PM
  3. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  4. iterator
    By venkatallu in forum Advanced Java
    Replies: 3
    Last Post: 09-23-2008, 01:32 PM
  5. Iterator
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 02:07 PM

Posting Permissions

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