{"record":{"id":"9826cf82a452a89a","repo":"apache/hadoop","slug":"the-genericwritable-has-not-been-set-correctly-ty","errorCode":null,"errorMessage":"The GenericWritable has NOT been set correctly. type={type}, instance={instance}","messagePattern":"The GenericWritable has NOT been set correctly\\. type=(.+?), instance=(.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/GenericWritable.java","lineNumber":140,"sourceCode":"  }\n\n  @Override\n  public void readFields(DataInput in) throws IOException {\n    type = in.readByte();\n    Class<? extends Writable> clazz = getTypes()[type & 0xff];\n    try {\n      instance = ReflectionUtils.newInstance(clazz, conf);\n    } catch (Exception e) {\n      e.printStackTrace();\n      throw new IOException(\"Cannot initialize the class: \" + clazz);\n    }\n    instance.readFields(in);\n  }\n\n  @Override\n  public void write(DataOutput out) throws IOException {\n    if (type == NOT_SET || instance == null)\n      throw new IOException(\"The GenericWritable has NOT been set correctly. type=\"\n                            + type + \", instance=\" + instance);\n    out.writeByte(type);\n    instance.write(out);\n  }\n\n  /**\n   * Return all classes that may be wrapped.  Subclasses should implement this\n   * to return a constant array of classes.\n   * @return all classes that may be wrapped.\n   */\n  abstract protected Class<? extends Writable>[] getTypes();\n\n  @Override\n  public Configuration getConf() {\n    return conf;\n  }\n\n  @Override","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/GenericWritable.java#L122-L158","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Call set(instance) before write() on every record path.","For genuinely absent values, skip writing the field or encode nullability explicitly (e.g. a BooleanWritable present-flag) — GenericWritable cannot represent null.","Add an isSet-style guard (type != NOT_SET / get() != null) before serializing."],"exampleFix":"// before\nMyGenericWritable w = new MyGenericWritable();\nw.write(out); // throws: never set\n\n// after\nMyGenericWritable w = new MyGenericWritable();\nw.set(new Text(\"value\"));\nw.write(out);","handlingStrategy":"validation","validationCode":"if (gw.get() == null) {\n  gw.set(defaultValue); // or skip writing this field\n}\ngw.write(out);","typeGuard":null,"tryCatchPattern":"try {\n  gw.write(out);\n} catch (IOException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"NOT been set\")) {\n    throw new IllegalStateException(\"Polymorphic field never populated before write\", e);\n  }\n  throw e;\n}","preventionTips":["Call set() on every code path that writes the writable.","Encode optional polymorphic fields with an explicit presence flag; GenericWritable cannot represent null."],"tags":["hadoop","serialization","writable","uninitialized-state","validation"],"backgroundTag":"uninitialized-object-serialization","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}