-1

I am getting a null pointer exception on the two while loops. I don't know why I am getting that error, can someone please look at the code and let me know why I am getting that error. I have posted my code below, along with the test code I am using. Any help would be greatly appreciated.

private MyNode partitionHelper(MyNode inHeader, MyNode inTrailer) {

        // Assume the numbers array is global
        T pivot = inHeader.data;
        MyNode leftPos = inHeader;
        MyNode rightPos = inTrailer;
        while (true) {
            // Increment l while numbers[l] < pivot
            while ((leftPos.data).compareTo(pivot) > 0) {
                leftPos = leftPos.next;
            }
            // Decrement h while pivot < numbers[h]
            while ((pivot).compareTo(rightPos.data) > 0) {
                rightPos = rightPos.prev;
            } // If there are zero or one elements remaining,
            // all numbers are partitioned. Return h
            if (leftPos == rightPos || rightPos.next == leftPos) {
                return rightPos;
            } // Swap numbers[l] and numbers[h],
            // update l and h 
            else {


                leftPos.next = rightPos.next;
                rightPos.prev = leftPos.prev;

                if (leftPos.prev == rightPos) {
                    leftPos.prev = rightPos;
                    rightPos.next = leftPos;
                } else {
                    leftPos.prev = rightPos.prev;
                    rightPos.next = leftPos.next;


                }  
                leftPos = leftPos.next;
                rightPos = rightPos.prev;
            }

        }
    }

   ll.add(21016);
        ll.add(25326);
        ll.add(9026);
        ll.add(1297);
        ll.add(17432);
        ll.add(30599);
        ll.add(21367);


        System.out.println(ll);
        int j = ll.partition();
        // partition returns the data, not the index, but you can
        // probably still see the partitions.
        System.out.println("j=" + j);
        System.out.println(ll);

    }
  • 2
    Can you show stacktrace? – Shevtsov Oct 19 '19 at 21:07
  • 1
    Hmmm, should question be closed with [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/q/218384/5221149) or with [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149). Maybe with [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/5221149) – Andreas Oct 19 '19 at 21:10
  • Please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), including error messages, stack-traces and a complete compileable source code. – Tom Drake Oct 19 '19 at 21:38

1 Answers1

0

probably your problem is here:

while ((leftPos.data).compareTo(pivot) > 0) {
      leftPos = leftPos.next;
}
while ((pivot).compareTo(rightPos.data) > 0) {
      rightPos = rightPos.prev;
}

You don't check is leftPos has next or rightPos has previos. So if your leftPos is last, then leftPos.next will be null and on next iteration of while loop you will try get leftPos.data - here you will get NullPointer.

You should check if next or prev is not null:

while ((leftPos.data).compareTo(pivot) > 0  && leftPos.next!=null) {
      leftPos = leftPos.next;
}
while ((pivot).compareTo(rightPos.data) > 0 && rightPos.prev!=null) {
      rightPos = rightPos.prev;
}
Victor1125
  • 642
  • 5
  • 16
  • any way, first loop is wrong: `while ((leftPos.data).compareTo(pivot) > 0) {...}`,because leftPos.data is this same object as pivot: `T pivot = inHeader.data` and `leftPos = inHeader`, so comparison will get 0, and you never get into the loop – Victor1125 Oct 19 '19 at 22:38