-1

I'm trying to implement a Java linked list. The program is giving me a null pointer whenever I try to delete the next to last node.

Here are the attributes:

public class Nodo {
  int num;
  boolean used;
  Nodo next;        
}

And this is the method so far:

    public int delete(int val) { // deletes, (1:deleted, 0:not found, -1:empty
                                // list)
    int deleted = 0;
    if (n.used) {
        if (n.next == null) {
            if (n.num == val) {
                n.used = false;
                deleted = 1;
            }
        } else {
            Nodo it = n;
            Nodo itAnt = null;
            while (it.next != null) {
                if (it.num == val) {
                    it.num = it.next.num;
                    it.next = it.next.next;
                    deleted = 1;
                }
                itAnt = it;
                it = it.next;
            }
            if (it.num == val) {
                itAnt.next = null;
                deleted = 1;
            }
        }

    } else {
        deleted = -1;
        System.out.println("Empty list");
    }
    return deleted;
}

How can I solve this problem?

Kara
  • 6,115
  • 16
  • 50
  • 57
SDV
  • 33
  • 1
  • 7
  • 1
    Who is `n` ? Which line throws an NPE ? Need some more context. – Dici Sep 07 '14 at 04:13
  • You're getting this exception at `itAnt.next = null`? This could happen if the loop `while (it.next != null)` was skipped. I guess you could initialize `itAnt` with `n` instead of null: `Nodo itAnt = n;`. This won't change the behaviour of your loop, but may fix your problem. – Tom Sep 07 '14 at 04:14
  • @Dici n is a node, the method is being called from a class that contains the first node, n. – SDV Sep 07 '14 at 04:54
  • @Tom Just tried it, won't budge. But thanks mate – SDV Sep 07 '14 at 04:55
  • 1
    Anyway, it doesn't look like a classic implementation of a linked list. You don't need any boolean `used`. You over-complicated that all. – Dici Sep 07 '14 at 04:57
  • 1
    Well .. where exactly occurs the NPE? – Tom Sep 07 '14 at 04:58
  • Please post the stacktrace and indicate which line causes the NullPointerException. – Code-Apprentice Sep 07 '14 at 07:07
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Code-Apprentice Sep 07 '14 at 07:07

1 Answers1

1

Of course this code will throw an exception. Here is the scenario:

There is a possibility that it will be the second to last node in the list and it.num will be equal to val. Now, after the first iteration this while loop will have the following values:

while (it.next != null) {
    if (it.num == val) {
        it.num = it.next.num;
        it.next = it.next.next;  
        // "it.next" will be null. because "it" was one to last in the list.
        deleted = 1;
    }
    itAnt = it;    // This is the last item in the list
    it = it.next; // Here "it" will be null, because "it.next" is null
}

When it is time for the second iteration, it.next will throw a NullPointerException, because it was null already.

It is also worth mentioning that you don't need this line: Nodo it = n; because it is an object reference and both n and it are pointing at the same object. So whatever you do on it, is directly effecting the n.

Sam
  • 7,252
  • 16
  • 46
  • 65
pms
  • 944
  • 12
  • 28