-1

We started working on lists in class and there was this task to separate numbers from a list without creating a new list. So I used sepNum tag along with first inside the list. However it returns nullPointer and I don't understand why. The nullPointer appears at 2nd line in separateNumbers method. Element first exists and is !null.

public void separateNumbers() {
    sepNum = first;
    while (sepNum.info < 0 || sepNum.info > 9)
        sepNum = sepNum.point;

    Element previous = sepNum;
    Element current = sepNum.point;
    while (current != null) {
        if (current.info < 0 || current.info > 9){
            previous.point = current.point;
            current = current.point;
        } else {
            previous = current.point;
            current = current.point;
        }
    }
}

Here's Element class:

     class Element {
        char info;
        Element point;

        public Element (char ch) {
            this.info=ch;
            this.point=null;
        }

        public String toString() {
            return info+(point == null ? " " + point : "");
        }
    }

I've been trying to figure this out for a few hours now. Can you guys please help?

  • 2
    Do you have the stack trace? If it happens on the 2nd line, that means `first` (whatever that is) is null. – Steve Smith Mar 24 '17 at 14:44
  • the value of `first` must be null - are you 100% certain that it is not the case. please feel free to add more proof(maybe the calling method) – Japu_D_Cret Mar 24 '17 at 14:46
  • 2
    @SteveSmith not necessarily. since the sepNum variable gets set to sepNum.point in the loop it is also possible that sepNum.point is null. The loop would then throw a NPE in a following iteration – OH GOD SPIDERS Mar 24 '17 at 14:47
  • 2
    @OH GOD SPIDERS True. Why do they never post the stack traces? – Steve Smith Mar 24 '17 at 14:48
  • @SteveSmith first is the stack trace. And it is definitely not null. I forgot to mention, it works when I change sepNum with first, but I need to print two different ones at the end.... – SkyAnthrax94 Mar 24 '17 at 14:49
  • I'd hazard a guess that you're chaining `Elements` together, but in your while loop you're not checking if you get to the end of the chain. – Steve Smith Mar 24 '17 at 14:51
  • @SkyAnthrax94 - *"first is the stack trace"*. There is no stacktrace in your question. – Stephen C Mar 24 '17 at 15:04
  • @SteveSmith ok yeah, I forgot to check. Thanks! – SkyAnthrax94 Mar 24 '17 at 15:14

1 Answers1

1

Your problem occurs the second time through the while loop. I have replaced the variables with their values in braces.

public void separateNumbers()
{
    sepNum = {info: 'a', point: null};
    while ({'a'} < 0 || {'a'} > 9)  //sepNum.info
        sepNum = {null};            //sepNum.point

You then go back around to the while loop again to test the condition:

while ({null}.info < 0 || {null}.info > 9)    //sepNum.info

Because you are deferencing null, you get a null pointer exception.

Michael
  • 41,989
  • 11
  • 82
  • 128