{"record":{"id":"2aa22eafdaf525c7","repo":"stanfordnlp/CoreNLP","slug":"type-mismatch-on-arg1-cannot-apply-this-to-i","errorCode":null,"errorMessage":"Type mismatch on arg1: Cannot apply ${this} to ${in}","messagePattern":"Type mismatch on arg1: Cannot apply (.+?) to (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/types/ValueFunctions.java","lineNumber":891,"sourceCode":"              (!(in.get(1).get() instanceof Class) && !(in.get(1).get() instanceof String))) {\n        return false;\n      }\n      return true;\n    }\n    @Override\n    public Value apply(Env env, List<Value> in) {\n      Value cmv = in.get(0);\n      Object field = in.get(1).get();\n      Class annotationFieldClass = null;\n      if (field instanceof String)  {\n        annotationFieldClass = EnvLookup.lookupAnnotationKey(env, (String) field);\n        if (annotationFieldClass == null) {\n          throw new IllegalArgumentException(\"Cannot get annotation field \" + field);\n        }\n      } else if (field instanceof Class) {\n        annotationFieldClass = (Class)  field;\n      } else {\n        throw new IllegalArgumentException(\"Type mismatch on arg1: Cannot apply \" + this + \" to \" + in);\n      }\n      if (cmv.get() instanceof CoreMap) {\n        CoreMap cm = (CoreMap) cmv.get();\n        if (in.size() >= 3) {\n          Value v = in.get(2);\n          Object annotationObject = (v != null)? v.get():null;\n          cm.set(annotationFieldClass, annotationObject);\n        }\n        Object obj = cm.get(annotationFieldClass);\n        return Expressions.createValue(annotationFieldClass.getName(), obj);\n      } else if (cmv.get() instanceof List) {\n        List<CoreMap> cmList = (List<CoreMap>) cmv.get();\n        if (in.size() >= 3) {\n          Value v = in.get(2);\n          Object annotationObject = (v != null)? v.get():null;\n          for (CoreMap cm:cmList) {\n            cm.set(annotationFieldClass, annotationObject);\n          }","sourceCodeStart":873,"sourceCodeEnd":909,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/types/ValueFunctions.java#L873-L909","documentation":"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.","triggerScenarios":"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.","commonSituations":"Expression rules quoting/nesting arguments incorrectly so the wrong value lands in arg1, dynamic expressions passing arbitrary objects, refactors changing argument order.","solutions":["Pass the annotation field as a String name or a Class annotation key only.","Reorder arguments if a value shifted into arg1 (arg0 must be the CoreMap value).","Add a checkArgs/validation step before apply to catch type errors early."],"exampleFix":"// before\nValue v = getFn.apply(env, Arrays.asList(cmValue, Expressions.createValue(42)));\n// after\nValue v = getFn.apply(env, Arrays.asList(cmValue, Expressions.createValue(CoreAnnotations.TextAnnotation.class)));","handlingStrategy":"type-guard","validationCode":"Object field = args.get(1).get();\nif (!(field instanceof String || field instanceof Class)) throw new IllegalArgumentException(\"arg1 must be String or Class\");","typeGuard":"boolean isAnnotationKeyArg(Object field) {\n    return field instanceof String || field instanceof Class;\n}","tryCatchPattern":"try {\n    v = getFn.apply(env, args);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Type mismatch on arg1\")) {\n        v = null; // fix arg1 type upstream\n    } else throw e;\n}","preventionTips":["Pass field names as quoted strings or Class literals only","Check argument order when constructing annotation function calls","Add checkArgs() validation before apply()"],"tags":["tokensregex","type-mismatch","annotation-key"],"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"}