apache/hadoop · error · IllegalArgumentException

Null element is not supported.

Error message

Null element is not supported.

What it means

LightWeightHashSet deliberately does not support null elements: getElement(null) - and therefore contains(null), which delegates to it - throws IllegalArgumentException instead of returning false/null like java.util.HashSet. There is no null slot semantics in the chained bucket array, so nulls are rejected up front.

Source

Thrown at hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/LightWeightHashSet.java:195

   * Check if the set contains given element
   *
   * @return true if element present, false otherwise.
   */
  @SuppressWarnings("unchecked")
  @Override
  public boolean contains(final Object key) {
    return getElement((T)key) != null;
  }
  
  /**
   * Return the element in this set which is equal to
   * the given key, if such an element exists.
   * Otherwise returns null.
   */
  public T getElement(final T key) {
    // validate key
    if (key == null) {
      throw new IllegalArgumentException("Null element is not supported.");
    }
    // find element
    final int hashCode = key.hashCode();
    final int index = getIndex(hashCode);
    return getContainedElem(index, key, hashCode);
  }

  /**
   * Check if the set contains given element at given index. If it
   * does, return that element.
   *
   * @return the element, or null, if no element matches
   */
  protected T getContainedElem(int index, final T key, int hashCode) {
    for (LinkedElement<T> e = entries[index]; e != null; e = e.next) {
      // element found
      if (hashCode == e.hashCode && e.element.equals(key)) {
        return e.element;

View on GitHub (pinned to 2add963021)

Solutions

  1. Guard the call: if (key != null && set.contains(key)).
  2. Filter nulls from the source collection before querying (stream().filter(Objects::nonNull)).
  3. If null membership is a real requirement, use java.util.HashSet or ConcurrentHashMap.newKeySet() instead.

Example fix

// before
if (inodeSet.contains(key)) { ... } // key == null -> IllegalArgumentException

// after
if (key != null && inodeSet.contains(key)) { ... }
Defensive patterns

Strategy: type-guard

Validate before calling

if (key != null && set.contains(key)) { ... }

Type guard

static <T> boolean isNonNullKey(T key) { return key != null; }
// usage: keys.stream().filter(MyGuards::isNonNullKey).forEach(k -> hit = set.contains(k));

Prevention

When it happens

Trigger: set.contains(null) or set.getElement(null) - typically when the probe key comes from data that can legitimately contain null (Arrays.asList(a, null), a map.get() miss fed forward, JSON-parsed nulls).

Common situations: Migrating code from HashSet to LightWeightHashSet for memory savings without auditing null paths; defensive membership checks on untrusted input; test fixtures containing nulls.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/4157580dcdbc1a83. Report an issue: GitHub.