apache/druid · warning · NullPointerException

BitSet cannot contain null values

Error message

BitSet cannot contain null values

What it means

IntegerSet.add rejects null elements: the underlying mutable bitmap (Roaring/concise-style) stores primitive ints and cannot represent a null Integer, so a NPE is thrown as an explicit contract enforcement rather than failing later with an opaque NPE.

Solutions

  1. Filter nulls before adding to the IntegerSet.
  2. Fix the caller that supplies boxed Integer values from a collection that may contain nulls.
  3. Use a structure that supports nulls if null representation is genuinely required.

Example fix

// before
set.add(maybeNull);
// after
if (maybeNull != null) { set.add(maybeNull); }
Defensive patterns

Strategy: validation

Validate before calling

// filter before addAll
List<Integer> safe = input.stream().filter(Objects::nonNull).collect(Collectors.toList());
set.addAll(safe);

Type guard

boolean isSafeForBitSet(Integer i) { return i != null && i >= 0; }

Try / catch

try {
  set.add(i);
} catch (NullPointerException e) {
  // null value supplied; skip or substitute default
}

Prevention

When it happens

Trigger: Adding a null Integer via add() or addAll() (which iterates add()), including automated tests testSimpleAdd/testIsEmpty/testIntOverflow that cover this path.

Common situations: Collecting integers from a data source that can yield nulls (nullable columns, absent lookups) and inserting them directly into an IntegerSet.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/71087125b079b009. Report an issue: GitHub.

Appendix: source

Thrown at processing/src/main/java/org/apache/druid/collections/IntegerSet.java:90

    return new BitSetIterator(mutableBitmap);
  }

  @Override
  public Object[] toArray()
  {
    Integer[] retval = new Integer[mutableBitmap.size()];
    int pos = 0;
    for (Integer i : this) {
      retval[pos++] = i;
    }
    return retval;
  }

  @Override
  public boolean add(Integer integer)
  {
    if (null == integer) {
      throw new NullPointerException("BitSet cannot contain null values");
    }
    if (integer < 0) {
      throw new IllegalArgumentException("Only positive integers or zero can be added");
    }
    boolean isSet = mutableBitmap.get(integer);
    mutableBitmap.add(integer.intValue());
    return !isSet;
  }

  @Override
  public boolean remove(Object o)
  {
    if (o == null) {
      throw new NullPointerException("BitSet cannot contain null values");
    }
    if (o instanceof Integer) {
      Integer integer = (Integer) o;
      boolean isSet = mutableBitmap.get(integer);

View on GitHub (pinned to 9b90983fd2)