{"record":{"id":"98ac15f31990c23b","repo":"apache/hadoop","slug":"input-array-can-not-be-null","errorCode":null,"errorMessage":"Input array can not be null","messagePattern":"Input array can not be null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/LightWeightHashSet.java","lineNumber":608,"sourceCode":"    this.expandThreshold = (int) (capacity * maxLoadFactor);\n    this.shrinkThreshold = (int) (capacity * minLoadFactor);\n\n    entries = new LinkedElement[capacity];\n    size = 0;\n    modification++;\n  }\n\n  @Override\n  public Object[] toArray() {\n    Object[] result = new Object[size];\n    return toArray(result);\n  }\n\n  @Override\n  @SuppressWarnings(\"unchecked\")\n  public <U> U[] toArray(U[] a) {\n    if (a == null) {\n      throw new NullPointerException(\"Input array can not be null\");\n    }\n    if (a.length < size) {\n      a = (U[]) java.lang.reflect.Array.newInstance(a.getClass()\n          .getComponentType(), size);\n    }\n    int currentIndex = 0;\n    for (int i = 0; i < entries.length; i++) {\n      LinkedElement<T> current = entries[i];\n      while (current != null) {\n        a[currentIndex++] = (U) current.element;\n        current = current.next;\n      }\n    }\n    return a;\n  }\n\n  @Override\n  public boolean containsAll(Collection<?> c) {","sourceCodeStart":590,"sourceCodeEnd":626,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/LightWeightHashSet.java#L590-L626","documentation":"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.","triggerScenarios":"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).","commonSituations":"Generic copy helpers with @Nullable array parameters; test code passing null to probe behavior; refactors from streams where the array supplier disappeared.","solutions":["Pass a concrete array: set.toArray(new T[0]) (or new T[set.size()] to avoid reallocation).","Null-check the array variable before the call and default it.","Use the no-arg set.toArray() (Object[]) when the component type does not matter."],"exampleFix":"// before\nString[] arr = set.toArray(maybeNull);\n\n// after\nString[] arr = set.toArray(new String[0]);","handlingStrategy":"validation","validationCode":"T[] dest = (arr != null) ? arr : newArray(size); // never pass null\nT[] result = set.toArray(dest);","typeGuard":"static <T> boolean isUsableArray(T[] a) { return a != null; } // guard before toArray()","tryCatchPattern":null,"preventionTips":["Standardize on set.toArray(new T[0]) at all call sites.","Mark array parameters @NonNull in utility signatures so null never reaches toArray.","Prefer the no-arg toArray() when a plain Object[] suffices."],"tags":["null-safety","toarray","null-pointer","set"],"backgroundTag":"null-argument-rejected","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}