0

Trying to remove a node by index now. I'd like to print out the list of nodes with indices so the user can select the index as seen. I think my logic is okay in printing the list with the indices but no input is coming out :(

At one point in fooling around with this, I was still unable to print the list of nodes but the "enter in the index you wish to delete" was output and was able to take a users' selection but eventually got a NullPointerException.

            else if (menu.equals("d")) {
            EntryNode temp = head;
            while (temp != null) {
                for (int i = 0; i < addressBook.length(); i++) {
                    //gets node at index
                    System.out.println(temp.getFirstName() + i);
                    temp = temp.getNext();
                }
            System.out.println(" ");
            System.out.println("Please enter the index of the entry you wish to delete ");
            int index = keyboard.nextInt();
            addressBook.removeEntry(index);
            }

        }

The removal method: public void removeEntry(int index){

    //delete from the head
    if (index == 0) {
        EntryNode temp = head;
        head = temp.getNext();
        temp.setNext(null);
        head.setPrev(null);
        size--;
    }
    //delete from the tail
    else if (index == length()) {
        EntryNode temp = tail;
        temp.setPrev(null);
        tail.setNext(null);
        tail = temp.getPrev();

        size--;
    }
    //in the middle
    else {
        EntryNode temp = head;
        for (int i = 0; i < index; i++) {
            //gets node at index
            temp = temp.getNext();
        }
        //set node after temp's previous to temp's previous 
        temp.getNext().setPrev(temp.getPrev());
        temp.getPrev().setNext(temp.getNext());
        temp.setNext(null);
        temp.setPrev(null);
        size--;
    }
}

The NullPointerException comes from :

//set node after temp's previous to temp's previous 
temp.getNext().setPrev(temp.getPrev());
1
  • Shouldn't else if (index == length()) { be else if (index == length() -1 ) { instead? If you start indexing from 0 and length() is the number of elements, the index of the tail will be length() - 1 and not length(). Commented Mar 9, 2012 at 15:39

2 Answers 2

2

You should check if temp.getNext() is not null before calling the setPrev() on it.

Also, you should check with length()-1 as you have nodes which are zero indexed.

Sign up to request clarification or add additional context in comments.

13 Comments

If temp.getNext() is null, isn't temp tail then?
Yes, but if you do your null checks properly, you won't have to have a special case for head and tail.
@JackieAldama Yes. Its going to in the middle block because your length is probably not correct (Is it returning actual-length (then modify your condition) or actual-length - 1?).
Okay I updated this to if, else statement to check if temp==head, temp.getNext() == null, or else...
Regarding the first part of the question though, is there something clearly wrong with the way I am trying to output the list with indices for user input?
|
1

Indices are in the range [0, length() - 1], so you should use

else if (index == length() - 1) {

instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.