{"record":{"id":"24fd4907a063317a","repo":"apache/beam","slug":"e-writablecoder","errorCode":null,"errorMessage":"${e}","messagePattern":"\\$\\{e\\}","errorType":"exception","errorClass":"CannotProvideCoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/hadoop-common/src/main/java/org/apache/beam/sdk/io/hadoop/WritableCoder.java","lineNumber":168,"sourceCode":"    private static final TypeDescriptor<Writable> WRITABLE_TYPE = new TypeDescriptor<Writable>() {};\n\n    @Override\n    public <T> Coder<T> coderFor(\n        TypeDescriptor<T> typeDescriptor, List<? extends Coder<?>> componentCoders)\n        throws CannotProvideCoderException {\n      if (!typeDescriptor.isSubtypeOf(WRITABLE_TYPE)) {\n        throw new CannotProvideCoderException(\n            String.format(\n                \"Cannot provide %s because %s does not implement the interface %s\",\n                WritableCoder.class.getSimpleName(), typeDescriptor, Writable.class.getName()));\n      }\n\n      try {\n        @SuppressWarnings(\"unchecked\")\n        Coder<T> coder = WritableCoder.of((Class) typeDescriptor.getRawType());\n        return coder;\n      } catch (IllegalArgumentException e) {\n        throw new CannotProvideCoderException(e);\n      }\n    }\n  }\n}\n","sourceCodeStart":150,"sourceCodeEnd":173,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/hadoop-common/src/main/java/org/apache/beam/sdk/io/hadoop/WritableCoder.java#L150-L173","documentation":"After confirming the type implements Writable, coderFor() calls WritableCoder.of(rawType). If that call fails with IllegalArgumentException (e.g. the class cannot be instantiated as required by WritableCoder, such as lacking a no-arg constructor), the exception is wrapped in a CannotProvideCoderException with the underlying message as the reason.","triggerScenarios":"Calling coderFor() with a Writable subclass that WritableCoder.of() rejects — typically a class without a public no-argument constructor required by Hadoop's ReflectionUtils-based instantiation.","commonSituations":"Custom Writable classes written with constructor arguments only; anonymous or inner classes without accessible no-arg constructors used as pipeline record types.","solutions":["Add a public no-argument constructor to the Writable class.","Register an explicit Coder for the type rather than using coderFor().","Inspect the wrapped IllegalArgumentException message for the exact rejection reason."],"exampleFix":"// before\npublic MyWritable(String value) { this.value = value; }\n// after\npublic MyWritable() {} // no-arg ctor for reflection\npublic MyWritable(String value) { this.value = value; }","handlingStrategy":"validation","validationCode":"if (java.lang.reflect.Modifier.isPublic(MyWritable.class.getConstructor().getModifiers()) == false) {\n  throw new IllegalStateException(\"Writable needs a public no-arg constructor\");\n}","typeGuard":"static boolean hasNoArgCtor(Class<?> cls) {\n  try { cls.getConstructor(); return true; } catch (NoSuchMethodException e) { return false; }\n}","tryCatchPattern":"try {\n  coder = WritableCoder.of(clazz);\n} catch (IllegalArgumentException e) {\n  throw new CannotProvideCoderException(e);\n}","preventionTips":["Give every Writable class a public no-arg constructor","Avoid anonymous/inner classes as record types","Unit-test coder round-trips for custom Writables"],"tags":["java","serialization","reflection","hadoop"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}