Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting Object with Comparator gives Null Pointer
    primarykey
    data
    text
    <p>I am trying sort the ArrayList with 3 Card in it. I am doing this with a Comparator. (Is this overkill)? Card.getRank() returns an integer between 2 and 14. I have absolutely no idea where I am going wrong. I have done this successfully before, and compared with my other code, and it seems the same. I would greatly appreciate if someone could spread some light on this!</p> <pre><code>public int getHand(Card c1, Card c2, Card c3) { ArrayList&lt;Card&gt; hand = new ArrayList&lt;Card&gt;(); hand.add(c1); hand.add(c2); hand.add(c3); Collections.sort(hand, new SortCards()); </code></pre> <hr> <pre><code> package com.evorlor.threecardpoker; import java.util.Comparator; public class SortCards implements Comparator&lt;Card&gt; { @Override public int compare(Card card1, Card card2) { if ((card1.getRank() - card2.getRank()) &gt; 0) { return 1; } if ((card1.getRank() - card2.getRank()) &lt; 0) { return -1; } return 0; } } </code></pre> <hr> <p>LOGCAT:</p> <pre><code>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) </code></pre> <hr> <pre><code>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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload