使用Comparator对对象进行排序会产生空指针

时间:2013-08-06 22:51:45

标签: java collections nullpointerexception comparator

我正在尝试使用3张卡对ArrayList进行排序。我是用比较器做的。 (这是矫枉过正的)? Card.getRank()返回2到14之间的整数。我完全不知道我哪里出错了。我以前成功完成了这个,并与我的其他代码进行了比较,看起来是一样的。如果有人能够对此有所了解,我将不胜感激!

public int getHand(Card c1, Card c2, Card c3) {

ArrayList<Card> hand = new ArrayList<Card>();
hand.add(c1);
hand.add(c2);
hand.add(c3);

Collections.sort(hand, new SortCards());

    package com.evorlor.threecardpoker;

    import java.util.Comparator;

    public class SortCards implements Comparator<Card> {

    @Override
    public int compare(Card card1, Card card2) {

        if ((card1.getRank() - card2.getRank()) > 0) {
            return 1;
        }
        if ((card1.getRank() - card2.getRank()) < 0) {
            return -1;
        }
        return 0;

    }

}

logcat的:

08-06 18:32:56.155: E/AndroidRuntime(4906): FATAL EXCEPTION: main
08-06 18:32:56.155: E/AndroidRuntime(4906): java.lang.IllegalStateException: Could not execute method of the activity
08-06 18:32:56.155: E/AndroidRuntime(4906):     at android.view.View$1.onClick(View.java:3599)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at android.view.View.performClick(View.java:4204)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at android.view.View$PerformClick.run(View.java:17355)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at android.os.Handler.handleCallback(Handler.java:725)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at android.os.Looper.loop(Looper.java:137)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at java.lang.reflect.Method.invoke(Method.java:511)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at dalvik.system.NativeStart.main(Native Method)
08-06 18:32:56.155: E/AndroidRuntime(4906): Caused by: java.lang.reflect.InvocationTargetException
08-06 18:32:56.155: E/AndroidRuntime(4906):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at java.lang.reflect.Method.invoke(Method.java:511)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at android.view.View$1.onClick(View.java:3594)
08-06 18:32:56.155: E/AndroidRuntime(4906):     ... 11 more
08-06 18:32:56.155: E/AndroidRuntime(4906): Caused by: java.lang.NullPointerException
08-06 18:32:56.155: E/AndroidRuntime(4906):     at com.evorlor.threecardpoker.SortCards.compare(SortCards.java:10)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at com.evorlor.threecardpoker.SortCards.compare(SortCards.java:1)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at java.util.TimSort.countRunAndMakeAscending(TimSort.java:320)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at java.util.TimSort.sort(TimSort.java:185)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at java.util.TimSort.sort(TimSort.java:169)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at java.util.Arrays.sort(Arrays.java:2038)
08-06 18:32:56.155: E/AndroidRuntime(4906):     at java.util.Collections.sort(Collections.java:1891)

package com.evorlor.threecardpoker;

public class Card {
    private int rank;
    private char suit;

    public Card(int rank, char suit) {
        super();
        this.rank = rank;
        this.suit = suit;
    }

    public int getRank() {
        return rank;
    }

    public char getSuit() {
        return suit;
    }

}

2 个答案:

答案 0 :(得分:2)

将来可以防止这种情况发生的一种方法是进行空检查。编码时,您需要确保处理这样的情况。一种方式可能是:

public int getHand(Card c1, Card c2, Card c3) {

    if((c1 != null) && (c2 != null) && (c3 != null)){
        ArrayList<Card> hand = new ArrayList<Card>();
        hand.add(c1);
        hand.add(c2);
        hand.add(c3);

        Collections.sort(hand, new SortCards());
    }else{
        #handle your null card situation here
        System.out.println("One or more card is null");
    }
}

答案 1 :(得分:-2)

传递给getHand(Card c1, Card c2, Card c3)的一张/多张卡片为空 您可以在getHand(Card c1, Card c2, Card c3)的第一行放置断言检查 如下所示

Assert.notNull(c1, "Card 1 is null");
Assert.notNull(c2, "Card 2 is null");
Assert.notNull(c3, "Card 3 is null");

OR

在调试器中执行它,同时在getHand(Card c1, Card c2, Card c3)

处设置断点