{"record":{"id":"6fa340b0560c2346","repo":"stanfordnlp/CoreNLP","slug":"unknown-field-s-for-type-typename-trying-to","errorCode":null,"errorMessage":"Unknown field ${s} for type ${typeName}, trying to set to ${v}","messagePattern":"Unknown field (.+?) 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":1277,"sourceCode":"          if (typeValue != null) {\n            // Check if variable points to a class\n            // 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 {","sourceCodeStart":1259,"sourceCodeEnd":1295,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java#L1259-L1295","documentation":"When evaluating a CompositeValue typed as a CLASS, the library instantiates the class and sets each attribute via reflection. If the attribute name does not correspond to any public Field on the target class, getField throws NoSuchFieldException, which is rethrown as this RuntimeException. It means the composite literal names a key the Java type does not have.","triggerScenarios":"Evaluating a composite expression like (CLASS) { key: value } where `key` is not a declared public field of the named class; typos in attribute names in TokensRegex rule composite literals; renaming a field in the target class without updating the rule.","commonSituations":"TokensRegex rules constructing annotation-like objects or custom result types where the rule author misspells a field or assumes a field exists that was removed/renamed in a CoreNLP version change.","solutions":["Fix the attribute name in the rule/expression to match an existing public field of the class.","Inspect the target class (or its public API) to see which fields are settable; note only public fields are found via getField.","If the field legitimately changed name in a newer CoreNLP version, update the rule or pin the library version.","Wrap evaluation in a try-catch for RuntimeException if the rule may reference optional fields, and drop unknown keys before evaluation."],"exampleFix":"// before\n(Entity) { nane: $name }  // typo: no field 'nane'\n// after\n(Entity) { name: $name }  // matches public field Entity.name","handlingStrategy":"validation","validationCode":"for (String key : composite.getAttributeNames()) {\n  try { targetClass.getField(key); }\n  catch (NoSuchFieldException e) { throw new IllegalStateException(\"Rule references unknown field: \" + key); }\n}","typeGuard":"static boolean hasPublicField(Class<?> c, String name) {\n  try { return c.getField(name) != null; } catch (NoSuchFieldException e) { return false; }\n}","tryCatchPattern":"try { Value v = composite.evaluate(env, args); }\ncatch (RuntimeException ex) {\n  if (ex.getMessage().startsWith(\"Unknown field\")) {\n    log.error(\"Rule field mismatch: \" + ex.getMessage(), ex);\n  }\n  throw ex;\n}","preventionTips":["Keep field names in rules in sync with the target class's public fields","Use fully qualified types and consult the class source when authoring rules","Pin CoreNLP version and re-test rules after upgrades"],"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"}