stanfordnlp/CoreNLP · error · IllegalArgumentException

Type mismatch on arg1: Cannot apply

Error message

Type mismatch on arg1: Cannot apply ${this} to ${in}

What it means

The annotation-value function expects arg1 to be either a String resolvable to an annotation key or a Class; any other type throws this IllegalArgumentException. It indicates the second argument's type is wrong for field lookup.

Solutions

  1. Pass the annotation field as a String name or a Class annotation key only.
  2. Reorder arguments if a value shifted into arg1 (arg0 must be the CoreMap value).
  3. Add a checkArgs/validation step before apply to catch type errors early.

Example fix

// before
Value v = getFn.apply(env, Arrays.asList(cmValue, Expressions.createValue(42)));
// after
Value v = getFn.apply(env, Arrays.asList(cmValue, Expressions.createValue(CoreAnnotations.TextAnnotation.class)));
Defensive patterns

Strategy: type-guard

Validate before calling

Object field = args.get(1).get();
if (!(field instanceof String || field instanceof Class)) throw new IllegalArgumentException("arg1 must be String or Class");

Type guard

boolean isAnnotationKeyArg(Object field) {
    return field instanceof String || field instanceof Class;
}

Try / catch

try {
    v = getFn.apply(env, args);
} catch (IllegalArgumentException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Type mismatch on arg1")) {
        v = null; // fix arg1 type upstream
    } else throw e;
}

Prevention

When it happens

Trigger: Passing a non-String/non-Class object as arg1, e.g. GET_ANNOTATION_VALUE(cm, 42) with a numeric literal, a List, or a Value wrapping an unexpected object.

Common situations: Expression rules quoting/nesting arguments incorrectly so the wrong value lands in arg1, dynamic expressions passing arbitrary objects, refactors changing argument order.

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/2aa22eafdaf525c7. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/types/ValueFunctions.java:891

              (!(in.get(1).get() instanceof Class) && !(in.get(1).get() instanceof String))) {
        return false;
      }
      return true;
    }
    @Override
    public Value apply(Env env, List<Value> in) {
      Value cmv = in.get(0);
      Object field = in.get(1).get();
      Class annotationFieldClass = null;
      if (field instanceof String)  {
        annotationFieldClass = EnvLookup.lookupAnnotationKey(env, (String) field);
        if (annotationFieldClass == null) {
          throw new IllegalArgumentException("Cannot get annotation field " + field);
        }
      } else if (field instanceof Class) {
        annotationFieldClass = (Class)  field;
      } else {
        throw new IllegalArgumentException("Type mismatch on arg1: Cannot apply " + this + " to " + in);
      }
      if (cmv.get() instanceof CoreMap) {
        CoreMap cm = (CoreMap) cmv.get();
        if (in.size() >= 3) {
          Value v = in.get(2);
          Object annotationObject = (v != null)? v.get():null;
          cm.set(annotationFieldClass, annotationObject);
        }
        Object obj = cm.get(annotationFieldClass);
        return Expressions.createValue(annotationFieldClass.getName(), obj);
      } else if (cmv.get() instanceof List) {
        List<CoreMap> cmList = (List<CoreMap>) cmv.get();
        if (in.size() >= 3) {
          Value v = in.get(2);
          Object annotationObject = (v != null)? v.get():null;
          for (CoreMap cm:cmList) {
            cm.set(annotationFieldClass, annotationObject);
          }

View on GitHub (pinned to 1b7edd19c4)