Note that there are some explanatory texts on larger screens.

plurals
  1. POComparison method violates its general contract! Java 7 only
    primarykey
    data
    text
    <p>I know this has been an issue for a while now, and checked all previously answers I could get, but still this one doesn't work.</p> <p>The object 'crew' represents crewmembers with ranks and other items. The comparison should be made by comparing 'assigned_rank', an int value, and if this value is equal in both instances, then 'is_trainer', a boolean, should make the difference.</p> <p>This method worked great as long as it was running with java &lt; 7. But since Java 7 I keep getting this one:</p> <pre><code>java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.ComparableTimSort.mergeLo(ComparableTimSort.java:714) at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:451) at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:376) at java.util.ComparableTimSort.sort(ComparableTimSort.java:182) at java.util.ComparableTimSort.sort(ComparableTimSort.java:146) at java.util.Arrays.sort(Arrays.java:472) at java.util.Collections.sort(Collections.java:155) at dormas_flightlog.Query.getCrew(Query.java:714) </code></pre> <p>Here is the source, where some potentially dangerous parts have allready been out-commented, but it still does not work:</p> <pre><code>public class crew implements Serializable, Comparable&lt;crew&gt; { private static final long serialVersionUID = 36L; private int flightID = 0; private int assigned_rank = 25; private boolean is_trainer = false; ... @Override public int compareTo(crew him) { int myRank = this.getAssigned_rank(); int hisRank = him.assigned_rank; if (this == him) { return 0; } if (myRank &gt; hisRank) { return 1; } if (myRank &lt; hisRank) { return -1; } if (myRank == hisRank) { // if (is_trainer &amp;&amp; !o.is_trainer) { // i = 1; // } // if (!is_trainer &amp;&amp; o.is_trainer) { // i = -1; // } // if (is_trainer &amp;&amp; o.is_trainer) { // i = 0; // } // if (!is_trainer &amp;&amp; !o.is_trainer) { // i = 0; // } return 0; } return 0; } @Override public int hashCode() { int hash = 7; hash = 31 * hash + this.assigned_rank; hash = 31 * hash + (this.is_trainer ? 1 : 0); return hash; } @Override public boolean equals(Object o) { if (this == o) { return true; } int myRank = this.getAssigned_rank(); int hisRank = 0; if (o instanceof crew) { crew him = (crew) o; hisRank = him.assigned_rank; } else { return false; } if (myRank &gt; hisRank) { return false; } if (myRank &lt; hisRank) { return false; } if (myRank == hisRank) { // if (is_trainer &amp;&amp; !o.is_trainer) { // i = 1; // } // if (!is_trainer &amp;&amp; o.is_trainer) { // i = -1; // } // if (is_trainer &amp;&amp; o.is_trainer) { // i = 0; // } // if (!is_trainer &amp;&amp; !o.is_trainer) { // i = 0; // } return true; } return false; } </code></pre> <p>}</p> <p>Implementing equals() was just a try to solve this problem. The given exception comes with or without equals(). I cannot see how the compareTo-method violates its contract. Any help is greatly appreciated....one day this code has to work with java 7 and I don't know how... Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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