0

I am getting the Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Comparison method violates its general contract! for the code below:

@Override
public int compareTo(Employee o) {

    if ((this.getStatus() == 1 && this.getSendBack() == 1)
            && (o.getStatus() == 1 && o.getSendBack() == 0))
        return 1; // greater than
    else if (this.getStatus() == 1 && o.getStatus() > 1)
        return 0; // equal to
    return -1; // less than
}

Additionally, this code is working for some of the conditions but it failing for some others. Moreover, I think this problem arises when I have some of the contradictory statements (please correct me if I am wrong), but I don't see having the same here.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rishabh Bansal
  • 362
  • 3
  • 7
  • 15
  • 4
    Does this answer your question? [Java error: Comparison method violates its general contract](https://stackoverflow.com/questions/11441666/java-error-comparison-method-violates-its-general-contract) – Hülya Jan 04 '21 at 09:03
  • 3
    `x.compareTo(x)` should be zero. If `x.compareTo(y) < 0` then `y.compareTo(x)` should be `> 0`. And if `x.compareTo(y) < 0` and `y.compareTo(z) < 0` then `x.compareTo(z)` should be `< 0`. Your comparison code doesn't look like it follows any of those rules. – khelwood Jan 04 '21 at 09:04

0 Answers0