apache/hadoop · error · IOException

The GenericWritable has NOT been set correctly. type={type},

Error message

The GenericWritable has NOT been set correctly. type={type}, instance={instance}

What it means

write() encodes the type byte of the wrapped instance, so it refuses to serialize a GenericWritable that was never populated: if type is still NOT_SET (-1) or instance is null, it throws IOException. A GenericWritable must have set(Writable) called (directly or via the state established before write) before serialization.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/GenericWritable.java:140

  }

  @Override
  public void readFields(DataInput in) throws IOException {
    type = in.readByte();
    Class<? extends Writable> clazz = getTypes()[type & 0xff];
    try {
      instance = ReflectionUtils.newInstance(clazz, conf);
    } catch (Exception e) {
      e.printStackTrace();
      throw new IOException("Cannot initialize the class: " + clazz);
    }
    instance.readFields(in);
  }

  @Override
  public void write(DataOutput out) throws IOException {
    if (type == NOT_SET || instance == null)
      throw new IOException("The GenericWritable has NOT been set correctly. type="
                            + type + ", instance=" + instance);
    out.writeByte(type);
    instance.write(out);
  }

  /**
   * Return all classes that may be wrapped.  Subclasses should implement this
   * to return a constant array of classes.
   * @return all classes that may be wrapped.
   */
  abstract protected Class<? extends Writable>[] getTypes();

  @Override
  public Configuration getConf() {
    return conf;
  }

  @Override

View on GitHub (pinned to 2add963021)

Solutions

  1. Call set(instance) before write() on every record path.
  2. For genuinely absent values, skip writing the field or encode nullability explicitly (e.g. a BooleanWritable present-flag) — GenericWritable cannot represent null.
  3. Add an isSet-style guard (type != NOT_SET / get() != null) before serializing.

Example fix

// before
MyGenericWritable w = new MyGenericWritable();
w.write(out); // throws: never set

// after
MyGenericWritable w = new MyGenericWritable();
w.set(new Text("value"));
w.write(out);
Defensive patterns

Strategy: validation

Validate before calling

if (gw.get() == null) {
  gw.set(defaultValue); // or skip writing this field
}
gw.write(out);

Try / catch

try {
  gw.write(out);
} catch (IOException e) {
  if (e.getMessage() != null && e.getMessage().contains("NOT been set")) {
    throw new IllegalStateException("Polymorphic field never populated before write", e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Creating the writable with the default constructor and immediately calling write(); reusing an instance across records where a null field path skips set(); serialization frameworks that reflectively instantiate writables and write them uninitialized.

Common situations: Optional polymorphic fields: record has no value for that column, code still writes the writable; mapper loop reusing one writable but a branch skips set() for some records.

Related errors


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