stanfordnlp/CoreNLP · error · RuntimeException
Error -- can't setVar to a different string -- old: " +…
Error message
Error -- can't setVar to a different string -- old: " + oldString + " new: " + string
What it means
VariableStrings.setVar throws RuntimeException if the variable already had a binding and the new string differs from the old one. Re-setting a variable to the SAME string is allowed (and increments the set counter), but contradicting an existing binding is treated as a programming error.
Solutions
- Ensure each variable maps to exactly one value; restructure so conflicting keys are namespaced or the instance is recreated per document.
- Call unsetVar before rebinding if the overwrite is intentional.
- Call isVarSet(var) first and skip/branch when already bound to a different value.
- Catch RuntimeException at the call site to detect the conflict.
Example fix
// before
vs.setVar("$author", doc1Author);
vs.setVar("$author", doc2Author); // throws
// after
if (vs.isVarSet("$author") && !doc2Author.equals(currentAuthor)) {
vs = new VariableStrings(...); // fresh bindings per document
}
vs.setVar("$author", doc2Author); Defensive patterns
Strategy: try-catch
Validate before calling
// check before rebinding
if (vs.isVarSet(var) && !oldValue.equals(newValue)) {
throw new IllegalStateException("var " + var + " already bound to " + oldValue);
} Try / catch
try {
vs.setVar(var, value);
} catch (RuntimeException e) {
if (e.getMessage().startsWith("Error -- can't setVar")) {
vs = new VariableStrings(...); // fresh bindings
} else throw e;
} Prevention
- Create a fresh VariableStrings per document/template.
- Namespace variables to avoid cross-document key collisions.
- Call unsetVar when an intentional rebinding is required.
- Check isVarSet before setting.
When it happens
Trigger: Calling setVar("$name", "Alice") then later setVar("$name", "Bob") on the same VariableStrings instance; reusing one instance across documents where templates share variable names but different replacements.
Common situations: Batch text templating over a corpus where the same placeholder appears with different intended values; accidental reuse of a populated VariableStrings object for a second pass.
Understand the failure class
Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.
Related errors
- Attempt to create ChineseSimWordAvgDepGrammar before…
- attempt to get word when sentence and lattice are null!
- segmentWords must be run first
- addFeature was called with a features object that is…
- adjustFinalToken: Unexpected final char: |
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/70f0d41b2b4c0b7c.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/util/VariableStrings.java:40
public VariableStrings(VariableStrings other) {
varsToStrings = new ArrayMap<>(other.varsToStrings);
numVarsSet = new IntCounter<>(other.numVarsSet);
}
public void reset() {
numVarsSet.clear();
varsToStrings.clear();
}
public boolean isSet(String o) {
return numVarsSet.getCount(o) >= 1;
}
public void setVar(String var, String string) {
String oldString = varsToStrings.put(var,string);
if(oldString != null && ! oldString.equals(string))
throw new RuntimeException("Error -- can't setVar to a different string -- old: " + oldString + " new: " + string);
numVarsSet.incrementCount(var);
}
public void unsetVar(String var) {
if(numVarsSet.getCount(var) > 0)
numVarsSet.decrementCount(var);
if(numVarsSet.getCount(var)==0)
varsToStrings.put(var,null);
}
public String getString(String var) {
return varsToStrings.get(var);
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append("{");View on GitHub (pinned to 1b7edd19c4)