Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

java - NullPointerException when comparing the data in two nodes

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...