Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7611013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:33:35+00:00 2026-05-31T01:33:35+00:00

Trying to remove a node by index now. I’d like to print out the

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:33:37+00:00Added an answer on May 31, 2026 at 1:33 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying out Joose with node.js and managed to produce code like this:
Im trying to remove a single node from a doubly threaded linked list. What
Trying to use exportDoc.Root.Elements(string).Where(node => !(node.Element(product).HasElements) || node.Element(product).Element(type).Value != product).Remove(); to remove the nodes
I'm trying out node.js and socket.io. I wan't to use to remove a ping
I am trying to figure out how to remove a node from a binary
I'm trying to remove a specific node from a XmlNodeList named listaWidths. This specific
I'm trying to remove multiple nodes specified by checkboxes after a dojo fadeout. The
i'm trying to remove a child node from an xml. my script is working..but
I'm trying to select all elements that have a given class and remove them
I use this code: I'm trying to remove the categoryPath node but keep its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.