stanfordnlp/CoreNLP · error · RuntimeException

Incompatible type for type , trying to set to

Error message

Incompatible type ${s} for type ${typeName}, trying to set to ${v}

What it means

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.

Solutions

  1. Convert the value to the field's declared type in the rule (e.g. quote removal, numeric literal instead of string).
  2. Check f.getType() (log it at design time) and make the rule literal match: Integer/Long for numeric fields, String for text fields.
  3. If you control the target class, add an overloaded setter or a create(CompositeValue) factory that performs conversion.
  4. Catch RuntimeException around evaluation and log the field/value to identify the offending pair quickly.

Example fix

// before
(Options) { verbose: "true" }  // field is boolean, value is String
// after
(Options) { verbose: true }    // boolean literal matches field type
Defensive patterns

Strategy: validation

Validate before calling

for (String key : composite.getAttributeNames()) {
  Class<?> ftype = targetClass.getField(key).getType();
  Object val = evaluateAttr(key);
  if (val != null && !ftype.isInstance(val) && !isBoxedCompatible(ftype, val)) {
    throw new IllegalStateException(key + " expects " + ftype + " but got " + val.getClass());
  }
}

Type guard

static boolean matchesFieldType(Class<?> ftype, Object v) {
  if (v == null) return true;
  Class<?> boxed = boxed(ftype); // int->Integer etc.
  return boxed.isInstance(v);
}

Try / catch

try { Value v = composite.evaluate(env, args); }
catch (RuntimeException ex) {
  if (ex.getMessage().startsWith("Incompatible type")) {
    throw new IllegalArgumentException("Fix literal type in rule: " + ex.getMessage(), ex);
  }
  throw ex;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/f3bf5295e5c220ac. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java:1279

            // If so, then try to instantiate a new instance of the class
            if (TYPE_CLASS.equals(typeValue.getType())) {
              // Variable maps to a java class
              Class c = (Class) typeValue.get();
              try {
                Object obj = c.newInstance();
                // for any field other than the "type", set the value of the field
                //   of the created object to the specified value
                for (String s:cv.value.keySet()) {
                  if (!"type".equals(s)) {
                    Value v = cv.value.get(s).evaluate(env, args);
                    try {
                      Field f = c.getField(s);
                      Object objVal =  toCompatibleObject(f, v.get());
                      f.set(obj, objVal);
                    } catch (NoSuchFieldException ex){
                      throw new RuntimeException("Unknown field " + s + " for type " + typeName + ", trying to set to " + v, ex);
                    } catch (IllegalArgumentException ex){
                      throw new RuntimeException("Incompatible type " + s + " for type " + typeName + ", trying to set to " + v, ex);
                    }
                  }
                }
                return new PrimitiveValue<>(typeName, obj);
              } catch (InstantiationException | IllegalAccessException ex) {
                throw new RuntimeException("Cannot instantiate " + c, ex);
              }
            } else if (typeValue.get() != null){
              // When evaluated, variable does not explicitly map to "CLASS"
              // See if we can convert this CompositeValue into appropriate object
              // by calling "create(CompositeValue cv)"
              Class c = typeValue.get().getClass();
              try {
                Method m = c.getMethod("create", CompositeValue.class);
                CompositeValue evaluatedCv = cv.evaluateNoTypeConversion(env, args);
                try {
                  return new PrimitiveValue<>(typeName, m.invoke(typeValue.get(), evaluatedCv));
                } catch (InvocationTargetException | IllegalAccessException ex) {

View on GitHub (pinned to 1b7edd19c4)