stanfordnlp/CoreNLP · error · AssertionError

Neither element of pair comparable

Error message

Neither element of pair comparable

What it means

Pair.compareTo compares first elements, falling back to second elements; if neither element of either pair is Comparable and the pairs are not equal, it throws AssertionError 'Neither element of pair comparable'. It signals that the pair cannot define a total order with the elements it holds.

Solutions

  1. Make the pair's element class implement Comparable (or use a Comparator) for ordering
  2. Sort with an explicit Comparator<Pair<T1,T2>> instead of relying on Pair's natural ordering
  3. Use a HashSet/HashMap rather than sorted collections if ordering is unnecessary
  4. Constrain generics to <T1 extends Comparable<T1>, ...> when creating the pairs

Example fix

// before
Collections.sort(pairs); // Pair<MyObj,String> where MyObj not Comparable
// after
pairs.sort(Comparator.comparing(Pair::first, myObjComparator));
Defensive patterns

Strategy: type-guard

Validate before calling

static <T1,T2> boolean pairComparable(Pair<T1,T2> p) {
  return p.first() instanceof Comparable || p.second() instanceof Comparable;
}

Type guard

if (!(p.first() instanceof Comparable) && !(p.second() instanceof Comparable)) {
  throw new IllegalArgumentException("Pair elements not comparable: " + p);
}

Try / catch

try {
  Collections.sort(pairs);
} catch (AssertionError e) {
  if (e.getMessage().contains("comparable")) {
    pairs.sort(Comparator.comparing(Pair::toString)); // explicit fallback order
  } else throw e;
}

Prevention

When it happens

Trigger: Sorting or inserting into a sorted collection a Pair whose first (and second) elements do not implement Comparable, so compareTo cannot order two unequal pairs.

Common situations: Calling Collections.sort on List<Pair<CustomType, X>> where CustomType has no compareTo; using Pairs as keys in TreeMap/TreeSet with non-comparable element types; generics where T1/T2 were not constrained to Comparable.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/eabc6dd43d89b9fa. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/util/Pair.java:156

   * @throws ClassCastException if the argument is not a
   *                            {@code Pair}.
   * @see java.lang.Comparable
   */
  @SuppressWarnings("unchecked")
  public int compareTo(Pair<T1,T2> another) {
    if (first() instanceof Comparable) {
      int comp = ((Comparable<T1>) first()).compareTo(another.first());
      if (comp != 0) {
        return comp;
      }
    }

    if (second() instanceof Comparable) {
      return ((Comparable<T2>) second()).compareTo(another.second());
    }

    if ((!(first() instanceof Comparable)) && (!(second() instanceof Comparable))) {
      throw new AssertionError("Neither element of pair comparable");
    }

    return 0;
  }

  /**
   * If first and second are Strings, then this returns an MutableInternedPair
   * where the Strings have been interned, and if this Pair is serialized
   * and then deserialized, first and second are interned upon
   * deserialization.
   *
   * @param p A pair of Strings
   * @return MutableInternedPair, with same first and second as this.
   */
  public static Pair<String, String> stringIntern(Pair<String, String> p) {
    return new MutableInternedPair(p);
  }

View on GitHub (pinned to 1b7edd19c4)