apache/beam · error · IllegalArgumentException

Can't compare %s with NULL

Error message

Can't compare %s with NULL

What it means

ContiguousSequenceRange.compareTo() refuses to compare against null, throwing IllegalArgumentException with the message "Can't compare <this> with NULL". Ranges are ordered by start then end, and a null has no defined order relative to a range, so the caller must guarantee non-null arguments.

Source

Thrown at sdks/java/extensions/ordered/src/main/java/org/apache/beam/sdk/extensions/ordered/ContiguousSequenceRange.java:56

    implements Serializable, Comparable<ContiguousSequenceRange> {

  public static final ContiguousSequenceRange EMPTY =
      ContiguousSequenceRange.of(
          Long.MIN_VALUE, Long.MIN_VALUE, Instant.ofEpochMilli(Long.MIN_VALUE));

  /** Returns inclusive starting sequence. */
  public abstract long getStart();

  /** Returns exclusive end sequence. */
  public abstract long getEnd();

  /** Returns latest timestamp of all events in the range. */
  public abstract Instant getTimestamp();

  @Override
  public int compareTo(ContiguousSequenceRange other) {
    if (other == null) {
      throw new IllegalArgumentException("Can't compare " + this + " with NULL");
    }

    int startCompare = Long.compare(this.getStart(), other.getStart());
    return startCompare == 0 ? Long.compare(this.getEnd(), other.getEnd()) : startCompare;
  }

  public static ContiguousSequenceRange largestRange(
      Iterable<ContiguousSequenceRange> rangeIterable) {
    ContiguousSequenceRange result = EMPTY;

    Iterator<ContiguousSequenceRange> iterator = rangeIterable.iterator();
    while (iterator.hasNext()) {
      ContiguousSequenceRange next = iterator.next();
      if (next.compareTo(result) > 0) {
        result = next;
      }
    }
    return result;

View on GitHub (pinned to 12126d8942)

Solutions

  1. Filter out nulls before sorting or reducing the collection of ranges.
  2. Check for null at the call site and skip/handle it before invoking compareTo.
  3. Fix upstream code so ContiguousSequenceRange instances are never left null in the collection.

Example fix

// before
ContiguousSequenceRange largest = ranges.stream().max(Comparable::compareTo).get();
// after
ContiguousSequenceRange largest = ranges.stream()
    .filter(java.util.Objects::nonNull)
    .max(Comparable::compareTo)
    .orElse(null);
Defensive patterns

Strategy: type-guard

Validate before calling

List<ContiguousSequenceRange> safe = ranges.stream().filter(Objects::nonNull).collect(Collectors.toList());
// now safe to sort / compareTo

Type guard

boolean isComparable(ContiguousSequenceRange r) { return r != null; }

Try / catch

try {
  int cmp = range.compareTo(other);
} catch (IllegalArgumentException e) {
  if (!e.getMessage().contains("with NULL")) throw e;
  // treat null as unmatched: skip or assign default ordering
}

Prevention

When it happens

Trigger: Calling range.compareTo(null) directly, or indirectly via sorting/min/max utilities (e.g. largestRange) when a collection contains a null element.

Common situations: Building lists of ContiguousSequenceRange from data that produced null entries, calling Collections.max()/Stream.min() on a list containing nulls, or passing an uninitialized variable into the comparator.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/abb3db87a7ec1bf9. Report an issue: GitHub.