{"record":{"id":"4157580dcdbc1a83","repo":"apache/hadoop","slug":"null-element-is-not-supported-415758","errorCode":null,"errorMessage":"Null element is not supported.","messagePattern":"Null element is not supported\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/LightWeightHashSet.java","lineNumber":195,"sourceCode":"   * Check if the set contains given element\n   *\n   * @return true if element present, false otherwise.\n   */\n  @SuppressWarnings(\"unchecked\")\n  @Override\n  public boolean contains(final Object key) {\n    return getElement((T)key) != null;\n  }\n  \n  /**\n   * Return the element in this set which is equal to\n   * the given key, if such an element exists.\n   * Otherwise returns null.\n   */\n  public T getElement(final T key) {\n    // validate key\n    if (key == null) {\n      throw new IllegalArgumentException(\"Null element is not supported.\");\n    }\n    // find element\n    final int hashCode = key.hashCode();\n    final int index = getIndex(hashCode);\n    return getContainedElem(index, key, hashCode);\n  }\n\n  /**\n   * Check if the set contains given element at given index. If it\n   * does, return that element.\n   *\n   * @return the element, or null, if no element matches\n   */\n  protected T getContainedElem(int index, final T key, int hashCode) {\n    for (LinkedElement<T> e = entries[index]; e != null; e = e.next) {\n      // element found\n      if (hashCode == e.hashCode && e.element.equals(key)) {\n        return e.element;","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/LightWeightHashSet.java#L177-L213","documentation":"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.","triggerScenarios":"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).","commonSituations":"Migrating code from HashSet to LightWeightHashSet for memory savings without auditing null paths; defensive membership checks on untrusted input; test fixtures containing nulls.","solutions":["Guard the call: if (key != null && set.contains(key)).","Filter nulls from the source collection before querying (stream().filter(Objects::nonNull)).","If null membership is a real requirement, use java.util.HashSet or ConcurrentHashMap.newKeySet() instead."],"exampleFix":"// before\nif (inodeSet.contains(key)) { ... } // key == null -> IllegalArgumentException\n\n// after\nif (key != null && inodeSet.contains(key)) { ... }","handlingStrategy":"type-guard","validationCode":"if (key != null && set.contains(key)) { ... }","typeGuard":"static <T> boolean isNonNullKey(T key) { return key != null; }\n// usage: keys.stream().filter(MyGuards::isNonNullKey).forEach(k -> hit = set.contains(k));","tryCatchPattern":null,"preventionTips":["Document that LightWeight* collections reject null in contains/getElement, unlike HashSet.","Filter nulls (Objects::nonNull) out of probe collections before membership tests.","Static analysis (NullAway/Checker Framework) on call sites feeding these sets."],"tags":["null-safety","set","illegal-argument","contains"],"backgroundTag":"null-argument-rejected","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}