{"record":{"id":"70f0d41b2b4c0b7c","repo":"stanfordnlp/CoreNLP","slug":"error-can-t-setvar-to-a-different-string-old","errorCode":null,"errorMessage":"Error -- can't setVar to a different string -- old: \" + oldString + \" new: \" + string","messagePattern":"Error -- can't setVar to a different string -- old: \" \\+ oldString \\+ \" new: \" \\+ string","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/VariableStrings.java","lineNumber":40,"sourceCode":"\n  public VariableStrings(VariableStrings other) {\n    varsToStrings = new ArrayMap<>(other.varsToStrings);\n    numVarsSet = new IntCounter<>(other.numVarsSet);\n  }\n\n  public void reset() {\n    numVarsSet.clear();\n    varsToStrings.clear();\n  }\n\n  public boolean isSet(String o) {\n    return numVarsSet.getCount(o) >= 1;\n  }\n\n  public void setVar(String var, String string) {\n    String oldString = varsToStrings.put(var,string);\n    if(oldString != null && ! oldString.equals(string))\n      throw new RuntimeException(\"Error -- can't setVar to a different string -- old: \" + oldString + \" new: \" + string);\n    numVarsSet.incrementCount(var);\n  }\n\n  public void unsetVar(String var) {\n    if(numVarsSet.getCount(var) > 0)\n      numVarsSet.decrementCount(var);\n    if(numVarsSet.getCount(var)==0)\n      varsToStrings.put(var,null);\n  }\n\n  public String getString(String var) {\n    return varsToStrings.get(var);\n  }\n\n  @Override\n  public String toString() {\n    StringBuilder s = new StringBuilder();\n    s.append(\"{\");","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/VariableStrings.java#L22-L58","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvs.setVar(\"$author\", doc1Author);\nvs.setVar(\"$author\", doc2Author); // throws\n// after\nif (vs.isVarSet(\"$author\") && !doc2Author.equals(currentAuthor)) {\n  vs = new VariableStrings(...); // fresh bindings per document\n}\nvs.setVar(\"$author\", doc2Author);","handlingStrategy":"try-catch","validationCode":"// check before rebinding\nif (vs.isVarSet(var) && !oldValue.equals(newValue)) {\n  throw new IllegalStateException(\"var \" + var + \" already bound to \" + oldValue);\n}","typeGuard":null,"tryCatchPattern":"try {\n  vs.setVar(var, value);\n} catch (RuntimeException e) {\n  if (e.getMessage().startsWith(\"Error -- can't setVar\")) {\n    vs = new VariableStrings(...); // fresh bindings\n  } else throw e;\n}","preventionTips":["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."],"tags":["java","state","template","variable"],"backgroundTag":"conflicting-config-options","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}