apache/beam · error · RuntimeException
Failed to instantiate object
Error message
Failed to instantiate object
What it means
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'.
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
Example fix
// before
class MyClass { private final String a; MyClass(String a) { this.a = a; } }
// after
class MyClass { private String a; public MyClass() {} public String getA() { return a; } public void setA(String a) { this.a = a; } } Defensive patterns
Strategy: type-guard
Validate before calling
Class<?> c = typeDescriptor.getRawType();
if (c.isInterface() || java.lang.reflect.Modifier.isAbstract(c.getModifiers())) throw new IllegalArgumentException(c + " not instantiable");
try { c.getDeclaredConstructor(); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(c + " lacks no-arg constructor"); } Type guard
boolean isInstantiable(Class<?> c) { try { c.getDeclaredConstructor(); return !c.isInterface() && !java.lang.reflect.Modifier.isAbstract(c.getModifiers()); } catch (NoSuchMethodException e) { return false; } } Try / catch
try { return schemaCreator.create(params); } catch (RuntimeException e) { throw new IllegalStateException("Cannot instantiate " + targetType + ": add no-arg ctor or @SchemaCreate", e); } Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Can not call prepareRun
- A method marked with SchemaCreate in class
- AutoValue builder class
- Cannot create UDF from method: method
- Cannot determine context class
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/61264d3b2df82bdf.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/SetterBasedCreatorFactory.java:51
public SetterBasedCreatorFactory(Factory<List<FieldValueSetter>> setterFactory) {
this.setterFactory = new CachingFactory<>(setterFactory);
}
@Override
public SchemaUserTypeCreator create(TypeDescriptor<?> typeDescriptor, Schema schema) {
List<FieldValueSetter> setters = setterFactory.create(typeDescriptor, schema);
return new SchemaUserTypeCreator() {
@Override
public Object create(Object... params) {
Object object;
try {
object = typeDescriptor.getRawType().getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException
| IllegalAccessException
| InvocationTargetException
| InstantiationException e) {
throw new RuntimeException("Failed to instantiate object ", e);
}
for (int i = 0; i < params.length; ++i) {
FieldValueSetter setter = setters.get(i);
setter.set(object, params[i]);
}
return object;
}
};
}
}
View on GitHub (pinned to 12126d8942)