{"record":{"id":"abb3db87a7ec1bf9","repo":"apache/beam","slug":"can-t-compare-s-with-null","errorCode":null,"errorMessage":"Can't compare %s with NULL","messagePattern":"Can't compare (.+?) with NULL","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/ordered/src/main/java/org/apache/beam/sdk/extensions/ordered/ContiguousSequenceRange.java","lineNumber":56,"sourceCode":"    implements Serializable, Comparable<ContiguousSequenceRange> {\n\n  public static final ContiguousSequenceRange EMPTY =\n      ContiguousSequenceRange.of(\n          Long.MIN_VALUE, Long.MIN_VALUE, Instant.ofEpochMilli(Long.MIN_VALUE));\n\n  /** Returns inclusive starting sequence. */\n  public abstract long getStart();\n\n  /** Returns exclusive end sequence. */\n  public abstract long getEnd();\n\n  /** Returns latest timestamp of all events in the range. */\n  public abstract Instant getTimestamp();\n\n  @Override\n  public int compareTo(ContiguousSequenceRange other) {\n    if (other == null) {\n      throw new IllegalArgumentException(\"Can't compare \" + this + \" with NULL\");\n    }\n\n    int startCompare = Long.compare(this.getStart(), other.getStart());\n    return startCompare == 0 ? Long.compare(this.getEnd(), other.getEnd()) : startCompare;\n  }\n\n  public static ContiguousSequenceRange largestRange(\n      Iterable<ContiguousSequenceRange> rangeIterable) {\n    ContiguousSequenceRange result = EMPTY;\n\n    Iterator<ContiguousSequenceRange> iterator = rangeIterable.iterator();\n    while (iterator.hasNext()) {\n      ContiguousSequenceRange next = iterator.next();\n      if (next.compareTo(result) > 0) {\n        result = next;\n      }\n    }\n    return result;","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/ordered/src/main/java/org/apache/beam/sdk/extensions/ordered/ContiguousSequenceRange.java#L38-L74","documentation":"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.","triggerScenarios":"Calling range.compareTo(null) directly, or indirectly via sorting/min/max utilities (e.g. largestRange) when a collection contains a null element.","commonSituations":"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.","solutions":["Filter out nulls before sorting or reducing the collection of ranges.","Check for null at the call site and skip/handle it before invoking compareTo.","Fix upstream code so ContiguousSequenceRange instances are never left null in the collection."],"exampleFix":"// before\nContiguousSequenceRange largest = ranges.stream().max(Comparable::compareTo).get();\n// after\nContiguousSequenceRange largest = ranges.stream()\n    .filter(java.util.Objects::nonNull)\n    .max(Comparable::compareTo)\n    .orElse(null);","handlingStrategy":"type-guard","validationCode":"List<ContiguousSequenceRange> safe = ranges.stream().filter(Objects::nonNull).collect(Collectors.toList());\n// now safe to sort / compareTo","typeGuard":"boolean isComparable(ContiguousSequenceRange r) { return r != null; }","tryCatchPattern":"try {\n  int cmp = range.compareTo(other);\n} catch (IllegalArgumentException e) {\n  if (!e.getMessage().contains(\"with NULL\")) throw e;\n  // treat null as unmatched: skip or assign default ordering\n}","preventionTips":["Never insert nulls into collections of ContiguousSequenceRange.","Use Optional<ContiguousSequenceRange> instead of null references.","Filter with Objects::nonNull before any sort, min, or max operation."],"tags":["java","apache-beam","comparator","null-check"],"backgroundTag":"null-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}