Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with using iterator remove on double linked list

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with using iterator remove on double linked list

    I am trying to remove data from a double linked list using the remove method using a iterator and I keep getting a null pointer exception.

    This is what I have

    System.out.print("Enter student ID: ");
    String studentID2 = in.next();
    Iterator<Student> iter = students.iterator();
    while (iter.hasNext()) {
    Student stu = (Student) iter.next();
    if(stu.getStudentID().equals(studentID2)){
    iter.remove();
    }
    }

    Says the error is in my remove method

    public void remove() {
    lastItemRet = lastItemRet.prev;
    lastItemRet.next = nextItem; The error is on this line.
    nextItem.prev = lastItemRet;
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with using iterator remove on double linked list

    Post an SSCCE and the full error code, or else you get guesses. My guess, nextItem is not defined anywhere or is out of scope of the location you are trying to access it.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with using iterator remove on double linked list

    The full error I get isThe full error I get is


    Exception in thread "main" java.lang.NullPointerException
    at DLList$Node.access$3(DLList.java:105)
    at DLList$ListIter.remove(DLList.java:182)
    at Menu.display(Menu.java:46)
    at Prog3.main(Prog3.java:17)

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with using iterator remove on double linked list

    What could be null on that line? Could lastItemRet be null? Add some println's in there to try and debug, or use a debugger to step through the code and see what is null. Without an SSCCE it is hard to comment on a fix.

Similar Threads

  1. linked list
    By javasohard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 02:22 AM
  2. Implementation of remove method in linkedlist Iterator
    By jay2you in forum Collections and Generics
    Replies: 3
    Last Post: October 12th, 2011, 09:16 AM
  3. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  4. linked list help
    By tjoney in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 3rd, 2011, 06:54 PM
  5. [SOLVED] Linked List Help
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2011, 10:32 PM