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
- 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.
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
- 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.
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
- Null element is not supported.
- Input array can not be null
- Path must be absolute: " + path
- Append is not supported by BaiduBosFileSystem
- min sleep time must be positive number and max sleep time mu
AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22).
Data as JSON: /api/errors/4157580dcdbc1a83.
Report an issue: GitHub.