karatelabs/karate · error · java.lang.RuntimeException
no such method
Error message
no such method: <setterName>
What it means
JavaUtils.set assigns a Java bean property from JS by constructing the setter name ('set' + capitalized field) and reflectively finding it. If no matching setter method exists it throws 'no such method: <setterName>', which is then converted to a JS typeError 'cannot set .<name>'.
Solutions
- Add a standard bean setter (setXxx) to the Java class, or use an existing mutator method explicitly
- Call the mutation method directly from JS instead of property assignment syntax
- Check property-name capitalization — the setter name is derived from the first letter capitalized
- If the object is meant to be immutable, construct a new instance with the desired value instead
Example fix
// before
obj.name = 'x'; // no such method: setName
// after
obj.setName('x'); // or add setName to the class Defensive patterns
Strategy: validation
Validate before calling
function hasSetter(obj, prop) {
var setter = 'set' + prop.charAt(0).toUpperCase() + prop.slice(1);
return obj[setter] !== undefined;
}
// call only if hasSetter(obj, 'name') Try / catch
try {
obj.name = value;
} catch (e) {
if (String(e).includes('cannot set')) karate.log('no bean setter for', 'name');
else throw e;
} Prevention
- Ensure Java classes expose standard bean setters
- Use explicit method calls instead of property assignment on Java objects
- Watch property-name capitalization when mapping JS names to setters
When it happens
Trigger: Setting a property on a Java object from JS where the bean has no setter for that name — e.g. `obj.someField = value` on a Java instance whose class lacks setSomeField, or the field is read-only/final.
Common situations: Assuming JS property assignment works like direct field access on Java objects; Java classes with fluent/builder APIs instead of bean setters; property name casing mismatches; records or immutable objects without setters.
Related errors
- <cause message>
- is not a constructor
- . is not a function (called on )
- no instance property:
- cannot get . (on )
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/6518100a12d3246f.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JavaUtils.java:267
return new JavaObject(object).getMethod(name);
}
try {
if (member instanceof Field field) {
return field.get(object);
}
return ((Method) member).invoke(object, EMPTY);
} catch (Exception e) {
throw JsErrorException.typeError("cannot get ." + name + " (on " + jsTypeName(object.getClass()) + ")");
}
}
static void set(Object object, String name, Object value) {
String setterName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
Object[] args = new Object[]{value};
try {
Method method = findMethod(object.getClass(), setterName, args);
if (method == null) {
throw new RuntimeException("no such method: " + setterName);
}
method.invoke(object, args);
} catch (Exception e) {
throw JsErrorException.typeError("cannot set ." + name + " (on " + jsTypeName(object.getClass()) + ")");
}
}
//==================================================================================================================
//
static final Object[] EMPTY = new Object[0];
private static Class<?>[] paramTypes(Object[] args) {
Class<?>[] paramTypes = new Class[args.length];
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
paramTypes[i] = arg == null ? Object.class : arg.getClass();
}
return paramTypes;View on GitHub (pinned to a22eb90246)