{"record":{"id":"1c173b9f72fde223","repo":"apache/hadoop","slug":"the-enumset-argument-is-null-or-is-an-empty-set-b","errorCode":null,"errorMessage":"The EnumSet argument is null, or is an empty set but with no elementType provided.","messagePattern":"The EnumSet argument is null, or is an empty set but with no elementType provided\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/EnumSetWritable.java","lineNumber":97,"sourceCode":"   */\n  public EnumSetWritable(EnumSet<E> value) {\n    this(value, null);\n  }\n\n  /**\n   * reset the EnumSetWritable with specified\n   * <code>value</code> and <code>elementType</code>. If the <code>value</code> argument\n   * is null or its size is zero, the <code>elementType</code> argument must not be\n   * null. If the argument <code>value</code>'s size is bigger than zero, the\n   * argument <code>elementType</code> is not be used.\n   * \n   * @param value enumSet Value.\n   * @param elementType elementType.\n   */\n  public void set(EnumSet<E> value, Class<E> elementType) {\n    if ((value == null || value.size() == 0)\n        && (this.elementType == null && elementType == null)) {\n      throw new IllegalArgumentException(\n          \"The EnumSet argument is null, or is an empty set but with no elementType provided.\");\n    }\n    this.value = value;\n    if (value != null && value.size() > 0) {\n      Iterator<E> iterator = value.iterator();\n      this.elementType = iterator.next().getDeclaringClass();\n    } else if (elementType != null) {\n      this.elementType = elementType;\n    }\n  }\n\n  /**\n   * Return the value of this EnumSetWritable.\n   * @return EnumSet.\n   */\n  public EnumSet<E> get() {\n    return value;\n  }","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/EnumSetWritable.java#L79-L115","documentation":"EnumSetWritable wraps an EnumSet for serialization and must know the enum element type to write it. set(value, elementType) throws IllegalArgumentException when value is null or empty AND neither the elementType argument nor a previously stored elementType is available — there is no way to derive the enum class from an empty set.","triggerScenarios":"new EnumSetWritable(null) (single-arg constructor delegates with null elementType); new EnumSetWritable(EnumSet.noneOf(Foo.class)); writable.set(null, null) after an instance that never learned a type.","commonSituations":"Wrapping optional enum-set fields that are legitimately empty; clearing a field with set(null, null) after constructing from an empty set; generic mapper code that always uses the one-arg constructor.","solutions":["Use the two-arg constructor for possibly-empty sets: new EnumSetWritable<>(value, MyEnum.class).","When clearing, pass the type: set(null, MyEnum.class) or set(EnumSet.noneOf(MyEnum.class), MyEnum.class).","Once an instance has held a non-empty set, elementType persists, so prefer populating a prototype with a real element or type at construction."],"exampleFix":"// before\nEnumSetWritable<Flag> w = new EnumSetWritable<>(flags); // throws when flags is null/empty\n\n// after\nEnumSetWritable<Flag> w = new EnumSetWritable<>(flags, Flag.class);","handlingStrategy":"validation","validationCode":"if ((value == null || value.isEmpty()) && elementType == null) {\n  throw new IllegalArgumentException(\"Empty EnumSet requires an element type\");\n}\nnew EnumSetWritable<>(value, elementType);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always use the two-arg constructor/set for possibly-empty enum sets.","Pass the enum class explicitly when clearing fields: set(null, MyEnum.class)."],"tags":["hadoop","serialization","writable","enumset","null-check"],"backgroundTag":"null-argument-validation","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}