Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
221 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...