apache/hadoop · error · NullPointerException

Input array can not be null

Error message

Input array can not be null

What it means

LightWeightLinkedSet.toArray(U[] a) mirrors the parent class behavior: an explicit NullPointerException with 'Input array can not be null' when the destination array is null, per the java.util contract. The walk then follows the linked list (insertion order), so only the null array - not ordering - can trigger this error.

Source

Thrown at hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/LightWeightLinkedSet.java:218

   * link list, don't worry about hashtable - faster version of the parent
   * method.
   */
  @Override
  public List<T> pollAll() {
    List<T> retList = new ArrayList<T>(size);
    while (head != null) {
      retList.add(head.element);
      head = head.after;
    }
    this.clear();
    return retList;
  }

  @Override
  @SuppressWarnings("unchecked")
  public <U> U[] toArray(U[] a) {
    if (a == null) {
      throw new NullPointerException("Input array can not be null");
    }
    if (a.length < size) {
      a = (U[]) java.lang.reflect.Array.newInstance(a.getClass()
          .getComponentType(), size);
    }
    int currentIndex = 0;
    DoubleLinkedElement<T> current = head;
    while (current != null) {
      T curr = current.element;
      a[currentIndex++] = (U) curr;
      current = current.after;
    }
    return a;
  }

  @Override
  public Iterator<T> iterator() {
    return new LinkedSetIterator();

View on GitHub (pinned to 2add963021)

Solutions

  1. Pass a typed empty array: set.toArray(new T[0]).
  2. Null-check and default the variable before the call.
  3. Fall back to the no-arg toArray() when Object[] is acceptable.

Example fix

// before
T[] out = set.toArray(arrOrNull);

// after
T[] out = set.toArray(arrOrNull != null ? arrOrNull : newArrayInstance(size));
Defensive patterns

Strategy: validation

Validate before calling

U[] dest = (a != null) ? a : (U[]) java.lang.reflect.Array.newInstance(componentType, set.size());
U[] result = set.toArray(dest);

Type guard

static <U> boolean isUsableArray(U[] a) { return a != null; } // guard before toArray()

Prevention

When it happens

Trigger: set.toArray(null) or passing a nullable array variable to toArray on a LightWeightLinkedSet.

Common situations: Same as the HashSet variant: optional/lazily-created array parameters, generic copy utilities, mocks returning null arrays.

Related errors


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