stanfordnlp/CoreNLP · error · IllegalArgumentException
2 arguments expected, got
Error message
2 arguments expected, got ${in.size()} What it means
The typed comparison ValueFunction (e.g. NUMBER compare) requires exactly two arguments and throws IllegalArgumentException when apply() receives any other count. The comparison produces TRUE/FALSE based on comparing the two values.
Solutions
- Supply exactly two arguments to the comparison function in the expression.
- Call checkArgs(in) before apply() and handle the false case.
- Fix dynamic expression builders so missing variables don't reduce the argument count.
Example fix
// before
Value v = ValueFunctions.of("GT").apply(env, Arrays.asList(a));
// after
Value v = ValueFunctions.of("GT").apply(env, Arrays.asList(a, b)); Defensive patterns
Strategy: validation
Validate before calling
if (args == null || args.size() != 2) throw new IllegalArgumentException("Compare needs exactly 2 args"); Type guard
boolean isBinary(List<Value> in) { return in != null && in.size() == 2; } Try / catch
try {
v = cmpFn.apply(env, args);
} catch (IllegalArgumentException e) {
if (e.getMessage() != null && e.getMessage().contains("2 arguments expected")) {
v = null; // handle missing operand
} else throw e;
} Prevention
- Always pass both operands to comparison functions
- Run checkArgs() before apply()
- Test generated expressions for missing operands
When it happens
Trigger: Calling a compare-style ValueFunction with in.size() != 2, e.g. apply(env, singletonList(v)) with one value or three values, or expression text like GT(5) missing an operand.
Common situations: Typos in expression strings omitting an operand, programmatic apply() calls without checkArgs, dynamically generated expressions where a variable evaluated to nothing and dropped an argument.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- Invalid number of arguments to
- arguments expected, got
- Annotation field cannot be null
- : Entry has multiple types for : . Taking type to be
- Attribute already defined:
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/c0c3b6d4f84cd3ac.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/tokensregex/types/ValueFunctions.java:787
public boolean checkArgs(List<Value> in) {
if (in.size() != 2) {
return false;
}
if (clazz != null) {
if (in.get(0) == null || in.get(0).get() == null || !(clazz.isAssignableFrom(in.get(0).get().getClass()))) {
return false;
}
if (in.get(1) == null || in.get(1).get() == null || !(clazz.isAssignableFrom(in.get(1).get().getClass()))) {
return false;
}
}
return true;
}
@Override
public Value apply(Env env, List<Value> in) {
if (in.size() != 2) {
throw new IllegalArgumentException("2 arguments expected, got " + in.size());
}
if (in.get(0) == null || in.get(1) == null || in.get(0).get() == null || in.get(1).get() == null) {
return null; // Can't compare...
}
Boolean res = compare((T) in.get(0).get(), (T) in.get(1).get());
return (res)? Expressions.TRUE: Expressions.FALSE;
}
}
public static final ValueFunction NOT_EQUALS_FUNCTION = new NamedValueFunction("EQUALS") {
@Override
public String getParamDesc() {
return "Object,Object";
}
@Override
public boolean checkArgs(List<Value> in) {
return in.size() == 2;View on GitHub (pinned to 1b7edd19c4)