-1

The task is to delete the nth node of a doubly-linked list. I drew it out, and I don't see the nullpointer error that's tripping me up.

I've created an if-statement to handle the edge case of an empty list. I created a while loop to traverse the linked list otherwise. I get there error when I'm attempting to set the node links. Details below.

    public double delete (int k) {

    int i = 0;
    Node temp = first;
    double retVal;

    if (k < 0 || k >= N) throw new IllegalArgumentException ();

    if (temp == null)
        return 0;

    while (temp != null) {

            if (i == k - 1) {
                temp.next = temp.next.next;
                temp.next.next.prev = temp;
                temp.next.next = null;
                temp.next.prev = null;
                return temp.next.item;
            } else {
                i++;
                temp = temp.next;
            }
        }

    return 0;

I was expecting to delete and return the node to be deleted. Instead I got a nullpointerexception at the line commented with "//Issue".

  • Post the stacktrace and tell us which object was null. – takendarkk May 24 '19 at 21:46
  • Hello! Are you getting the null pointer exception at this statement `temp.next = temp.next.next;`? Or else please post where you are getting it along with the stack trace as mentioned by @takendarkk – Ashish May 24 '19 at 21:50
  • @Ashish - hi! It actually identifies the line after that as the culprit. Temp.next.next.prev = temp – FLAN - Legacy May 24 '19 at 21:54
  • @takendarkk - the stack trace below follows a whole bunch of tests he put in there so I think it would be hard to tell which line it is without posting his test functions, and copying the whole thing exceeds the allotted response size here :(. Another code earlier actually identified temp.next.next.prev = temp as the culprit though. – FLAN - Legacy May 24 '19 at 21:57
  • @DennisKim Just a little advice (For thought) that doesn't in any way address the problem--if you created 3 methods instead of 1, it would break down beautifully. you might try defining Node getNodeAt(int node) and deleteNode(Node node). Those are both very simple to write and debug, then your delete(int) method is super simple to implement: deleteNode(int n){deleteNode(getNodeAt(n));} – Bill K May 24 '19 at 22:06
  • @BillK - thanks for the advice. We're not allowed to add any other methods to this class. It's unfortunate, but he (my prof) is trying to get us to learn it. However, I see your point and will try to break it down mentally. It seems as though I could just do that than code it all in one method, right? – FLAN - Legacy May 24 '19 at 22:11
  • @DennisKim You could code it with the different methods and then have your IDE "Inline" them... But I can't figure out why your professor would possible dissuade you from creating other methods--breaking down a problem into smaller chunks is one of the most important aspects of programming you can learn. – Bill K May 28 '19 at 21:17
  • @BillK - I think he's trying to get us to create from scratch as kind of a build-up learning strategy. Either that or he put so much work into his homework shell files that he doesn't want us to create much variation for grading purposes? This is only my second coding class, and it's concurrent with my C class which is frustrating but at least C and JAVA aren't too dissimilar. – FLAN - Legacy Jun 01 '19 at 01:22

1 Answers1

1

You have to check whether temp.next and temp.next.next exist. In your context we might have 1->2->3->null, and you might be referring to null.

Akra
  • 265
  • 2
  • 10
  • Ok, trying to code that in now to see what I get. – FLAN - Legacy May 24 '19 at 21:58
  • @DennisKim as a general rule of thumb, you want to to safety checks for your "next" pointers. if(temp.next) {do stuff with temp.next} – Akra May 24 '19 at 22:00
  • I wish I could upvote you, but I don't have enough rep. I usually have a tutor, but he's gone for the break. :( But your help was much appreciated! – FLAN - Legacy May 24 '19 at 22:15