{"record":{"id":"dc326f6f5351f622","repo":"apache/iceberg","slug":"arguments-cannot-both-be-null","errorCode":null,"errorMessage":"Arguments cannot both be null","messagePattern":"Arguments cannot both be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/apache/iceberg/util/ArrayUtil.java","lineNumber":293,"sourceCode":"   * This code is borrowed from `org.apache.commons:commons-lang3`.\n   *\n   * @param <T> the component type of the array\n   * @param array the array to \"add\" the element to, may be {@code null}\n   * @param element the object to add, may be {@code null}\n   * @return A new array containing the existing elements plus the new element The returned array\n   *     type will be that of the input array (unless null), in which case it will have the same\n   *     type as the element. If both are null, an IllegalArgumentException is thrown\n   * @since 2.1\n   * @throws IllegalArgumentException if both arguments are null\n   */\n  public static <T> T[] add(final T[] array, final T element) {\n    Class<?> type;\n    if (array != null) {\n      type = array.getClass().getComponentType();\n    } else if (element != null) {\n      type = element.getClass();\n    } else {\n      throw new IllegalArgumentException(\"Arguments cannot both be null\");\n    }\n    @SuppressWarnings(\"unchecked\") // type must be T\n    final T[] newArray = (T[]) copyArrayGrow1(array, type);\n    newArray[newArray.length - 1] = element;\n    return newArray;\n  }\n\n  /**\n   * Returns a copy of the given array of size 1 greater than the argument. The last value of the\n   * array is left to the default value.\n   *\n   * <p>This code is borrowed from `org.apache.commons:commons-lang3`.\n   *\n   * @param array The array to copy, must not be {@code null}.\n   * @param newArrayComponentType If {@code array} is {@code null}, create a size 1 array of this\n   *     type.\n   * @return A new copy of the array of size 1 greater than the input.\n   */","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/core/src/main/java/org/apache/iceberg/util/ArrayUtil.java#L275-L311","documentation":"ArrayUtil.add(array, element) determines the component type of the new array from either the existing array or the element. If both are null, no type can be inferred, so it throws IllegalArgumentException with 'Arguments cannot both be null'.","triggerScenarios":"Calling ArrayUtil.add(null, null) — e.g., accumulating into an array variable that was never initialized and whose first element is also null.","commonSituations":"Generic accumulation code where the array is lazily initialized to null and the element comes from a lookup that returned null; refactored code paths that pass through untyped nulls.","solutions":["Ensure at least one of the arguments is non-null before calling add","Initialize the target array with a concrete type instead of null","Provide an explicit element (or an empty array of the correct component type) for the first add"],"exampleFix":"// before\nT[] result = ArrayUtil.add(array, maybeNullElement);\n// after\nPreconditions.checkNotNull(maybeNullElement, \"element cannot be null\");\nT[] result = ArrayUtil.add(array, maybeNullElement);","handlingStrategy":"type-guard","validationCode":"if (array == null && element == null) throw new IllegalArgumentException(\"array and element cannot both be null\");","typeGuard":"static <T> boolean addable(T[] array, T element) { return array != null || element != null; }","tryCatchPattern":"try { result = ArrayUtil.add(array, element); } catch (IllegalArgumentException e) { /* initialize array with proper type */ }","preventionTips":["Initialize accumulation arrays with a concrete component type","Null-check the first element before the first add call","Prefer collections (List) over array-growing helpers during accumulation"],"tags":["arrays","null","argument-validation"],"backgroundTag":"null-argument","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}