{"record":{"id":"61264d3b2df82bdf","repo":"apache/beam","slug":"failed-to-instantiate-object","errorCode":null,"errorMessage":"Failed to instantiate object ","messagePattern":"Failed to instantiate object ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/SetterBasedCreatorFactory.java","lineNumber":51,"sourceCode":"\n  public SetterBasedCreatorFactory(Factory<List<FieldValueSetter>> setterFactory) {\n    this.setterFactory = new CachingFactory<>(setterFactory);\n  }\n\n  @Override\n  public SchemaUserTypeCreator create(TypeDescriptor<?> typeDescriptor, Schema schema) {\n    List<FieldValueSetter> setters = setterFactory.create(typeDescriptor, schema);\n    return new SchemaUserTypeCreator() {\n      @Override\n      public Object create(Object... params) {\n        Object object;\n        try {\n          object = typeDescriptor.getRawType().getDeclaredConstructor().newInstance();\n        } catch (NoSuchMethodException\n            | IllegalAccessException\n            | InvocationTargetException\n            | InstantiationException e) {\n          throw new RuntimeException(\"Failed to instantiate object \", e);\n        }\n        for (int i = 0; i < params.length; ++i) {\n          FieldValueSetter setter = setters.get(i);\n          setter.set(object, params[i]);\n        }\n        return object;\n      }\n    };\n  }\n}\n","sourceCodeStart":33,"sourceCodeEnd":62,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/SetterBasedCreatorFactory.java#L33-L62","documentation":"SetterBasedCreatorFactory.create builds schema-conformant objects by calling the no-arg constructor and then applying setters. If the target class has no accessible no-arg constructor (or the constructor throws), it wraps the reflective failure in a RuntimeException 'Failed to instantiate object'.","triggerScenarios":"Using schema-based conversion (e.g. SchemaCoder, DoFn schema transforms, fromRow/create) on a Java class without a public no-arg constructor, an abstract/interface type, or a constructor that throws (e.g. static init or constructor-side validation).","commonSituations":"Immutable classes with only builder/all-args constructors; inner non-static classes needing an enclosing instance; Kotlin/data classes without a no-arg ctor; missing public modifier on the constructor.","solutions":["Add a public no-arg constructor to the target class","Use a class with factory-method-based schema creation (@SchemaCreate) instead of setter-based instantiation","Make the class static/top-level so no enclosing instance is required","Inspect the wrapped cause (NoSuchMethodException/InstantiationException etc.) to identify the exact reflective failure"],"exampleFix":"// before\nclass MyClass { private final String a; MyClass(String a) { this.a = a; } }\n// after\nclass MyClass { private String a; public MyClass() {} public String getA() { return a; } public void setA(String a) { this.a = a; } }","handlingStrategy":"type-guard","validationCode":"Class<?> c = typeDescriptor.getRawType();\nif (c.isInterface() || java.lang.reflect.Modifier.isAbstract(c.getModifiers())) throw new IllegalArgumentException(c + \" not instantiable\");\ntry { c.getDeclaredConstructor(); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(c + \" lacks no-arg constructor\"); }","typeGuard":"boolean isInstantiable(Class<?> c) { try { c.getDeclaredConstructor(); return !c.isInterface() && !java.lang.reflect.Modifier.isAbstract(c.getModifiers()); } catch (NoSuchMethodException e) { return false; } }","tryCatchPattern":"try { return schemaCreator.create(params); } catch (RuntimeException e) { throw new IllegalStateException(\"Cannot instantiate \" + targetType + \": add no-arg ctor or @SchemaCreate\", e); }","preventionTips":["Always provide a public no-arg constructor on schema-converted classes","Prefer @SchemaCreate factory methods for immutable types","Avoid non-static inner classes as schema types"],"tags":["java","reflection","instantiation"],"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"}