apache/hadoop · error · NullPointerException

Input array can not be null

Error message

Input array can not be null

What it means

LightWeightHashSet.toArray(U[] a) adds an explicit null check with the message 'Input array can not be null', throwing NullPointerException - this matches the java.util contract (toArray(null) must throw NPE) but with a clearer diagnostic. Everything else about the method is standard: a too-small array is reallocated via reflection.

Source

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

    this.expandThreshold = (int) (capacity * maxLoadFactor);
    this.shrinkThreshold = (int) (capacity * minLoadFactor);

    entries = new LinkedElement[capacity];
    size = 0;
    modification++;
  }

  @Override
  public Object[] toArray() {
    Object[] result = new Object[size];
    return toArray(result);
  }

  @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;
    for (int i = 0; i < entries.length; i++) {
      LinkedElement<T> current = entries[i];
      while (current != null) {
        a[currentIndex++] = (U) current.element;
        current = current.next;
      }
    }
    return a;
  }

  @Override
  public boolean containsAll(Collection<?> c) {

View on GitHub (pinned to 2add963021)

Solutions

  1. Pass a concrete array: set.toArray(new T[0]) (or new T[set.size()] to avoid reallocation).
  2. Null-check the array variable before the call and default it.
  3. Use the no-arg set.toArray() (Object[]) when the component type does not matter.

Example fix

// before
String[] arr = set.toArray(maybeNull);

// after
String[] arr = set.toArray(new String[0]);
Defensive patterns

Strategy: validation

Validate before calling

T[] dest = (arr != null) ? arr : newArray(size); // never pass null
T[] result = set.toArray(dest);

Type guard

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

Prevention

When it happens

Trigger: set.toArray(null) directly, or set.toArray(a) where 'a' is a variable that is null on some path (e.g., lazily initialized, Optional-style plumbing, mock returns).

Common situations: Generic copy helpers with @Nullable array parameters; test code passing null to probe behavior; refactors from streams where the array supplier disappeared.

Related errors


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