{"record":{"id":"f3bf5295e5c220ac","repo":"stanfordnlp/CoreNLP","slug":"incompatible-type-s-for-type-typename-tryin","errorCode":null,"errorMessage":"Incompatible type ${s} for type ${typeName}, trying to set to ${v}","messagePattern":"Incompatible type (.+?) for type (.+?), trying to set to (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java","lineNumber":1279,"sourceCode":"            // If so, then try to instantiate a new instance of the class\n            if (TYPE_CLASS.equals(typeValue.getType())) {\n              // Variable maps to a java class\n              Class c = (Class) typeValue.get();\n              try {\n                Object obj = c.newInstance();\n                // for any field other than the \"type\", set the value of the field\n                //   of the created object to the specified value\n                for (String s:cv.value.keySet()) {\n                  if (!\"type\".equals(s)) {\n                    Value v = cv.value.get(s).evaluate(env, args);\n                    try {\n                      Field f = c.getField(s);\n                      Object objVal =  toCompatibleObject(f, v.get());\n                      f.set(obj, objVal);\n                    } catch (NoSuchFieldException ex){\n                      throw new RuntimeException(\"Unknown field \" + s + \" for type \" + typeName + \", trying to set to \" + v, ex);\n                    } catch (IllegalArgumentException ex){\n                      throw new RuntimeException(\"Incompatible type \" + s + \" for type \" + typeName + \", trying to set to \" + v, ex);\n                    }\n                  }\n                }\n                return new PrimitiveValue<>(typeName, obj);\n              } catch (InstantiationException | IllegalAccessException ex) {\n                throw new RuntimeException(\"Cannot instantiate \" + c, ex);\n              }\n            } else if (typeValue.get() != null){\n              // When evaluated, variable does not explicitly map to \"CLASS\"\n              // See if we can convert this CompositeValue into appropriate object\n              // by calling \"create(CompositeValue cv)\"\n              Class c = typeValue.get().getClass();\n              try {\n                Method m = c.getMethod(\"create\", CompositeValue.class);\n                CompositeValue evaluatedCv = cv.evaluateNoTypeConversion(env, args);\n                try {\n                  return new PrimitiveValue<>(typeName, m.invoke(typeValue.get(), evaluatedCv));\n                } catch (InvocationTargetException | IllegalAccessException ex) {","sourceCodeStart":1261,"sourceCodeEnd":1297,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java#L1261-L1297","documentation":"During reflection-based population of a class instance from a CompositeValue, Field.set throws IllegalArgumentException when the attribute value's Java type is not compatible with the declared field type. The library rethrows it with the field name, target type, and offending value.","triggerScenarios":"Evaluating (CLASS) { field: value } where value evaluates to a type not assignable to the field, e.g. assigning a String to an int field, or a String to a List; numeric/string literals in rules that don't match field declarations.","commonSituations":"Rule authors supplying string literals for numeric fields, or lists where a scalar is expected, when building annotation objects or custom types in TokensRegex rules.","solutions":["Convert the value to the field's declared type in the rule (e.g. quote removal, numeric literal instead of string).","Check f.getType() (log it at design time) and make the rule literal match: Integer/Long for numeric fields, String for text fields.","If you control the target class, add an overloaded setter or a create(CompositeValue) factory that performs conversion.","Catch RuntimeException around evaluation and log the field/value to identify the offending pair quickly."],"exampleFix":"// before\n(Options) { verbose: \"true\" }  // field is boolean, value is String\n// after\n(Options) { verbose: true }    // boolean literal matches field type","handlingStrategy":"validation","validationCode":"for (String key : composite.getAttributeNames()) {\n  Class<?> ftype = targetClass.getField(key).getType();\n  Object val = evaluateAttr(key);\n  if (val != null && !ftype.isInstance(val) && !isBoxedCompatible(ftype, val)) {\n    throw new IllegalStateException(key + \" expects \" + ftype + \" but got \" + val.getClass());\n  }\n}","typeGuard":"static boolean matchesFieldType(Class<?> ftype, Object v) {\n  if (v == null) return true;\n  Class<?> boxed = boxed(ftype); // int->Integer etc.\n  return boxed.isInstance(v);\n}","tryCatchPattern":"try { Value v = composite.evaluate(env, args); }\ncatch (RuntimeException ex) {\n  if (ex.getMessage().startsWith(\"Incompatible type\")) {\n    throw new IllegalArgumentException(\"Fix literal type in rule: \" + ex.getMessage(), ex);\n  }\n  throw ex;\n}","preventionTips":["Match literal types in rules to declared field types (boolean vs String, int vs double)","Avoid locale-formatted numbers in rule literals","Prefer create(CompositeValue) factories that normalize types"],"tags":["reflection","type-mismatch","tokensregex","composite-value"],"backgroundTag":"type-mismatch","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}