stanfordnlp/CoreNLP · error · RuntimeException

invalid format: ||

Error message

invalid format: ||

What it means

ClassicCounter.fromString expects the string produced by ClassicCounter.toString(): it must start with '{' and end with '}'. Otherwise it throws RuntimeException("invalid format: ||s||"). The || markers are just delimiters around the offending string in the message.

Solutions

  1. Ensure input is the exact toString() output of a ClassicCounter (with braces)
  2. Trim the string and strip a possible BOM before calling fromString
  3. If the data is TSV key/value pairs, use valueOfIgnoreComments instead of fromString

Example fix

// before
ClassicCounter<String> c = ClassicCounter.fromString(fileLine.trim() + ";");
// after
String s = fileLine.replace("\uFEFF", "").trim();
if (!s.startsWith("{") || !s.endsWith("}")) {
  throw new IllegalArgumentException("counter string must be {k=v, ...} form");
}
ClassicCounter<String> c = ClassicCounter.fromString(s);
Defensive patterns

Strategy: validation

Validate before calling

boolean isCounterString(String s) {
  String t = s == null ? "" : s.replace("\uFEFF", "").trim();
  return t.startsWith("{") && t.endsWith("}");
}

Try / catch

try {
  c = ClassicCounter.fromString(s);
} catch (RuntimeException e) {
  throw new IllegalArgumentException("expected toString() form {k=v, ...}", e);
}

Prevention

When it happens

Trigger: Calling ClassicCounter.fromString with a string that is not the toString() form of a counter — e.g. missing braces, empty string, whitespace-wrapped output, or a serialized file with corrupted first/last characters.

Common situations: Persisting a counter to a file and reading it back with encoding/BOM issues; concatenating counters incorrectly; passing JSON or TSV text to fromString by mistake.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/dda8f21bbeaaedbe. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/stats/ClassicCounter.java:590

        result.setCount(fields[0], Double.parseDouble(fields[1]));
      }
      return result;
    }


  /**
   * Converts from the format printed by the toString method back into
   * a Counter&lt;String&gt;.  The toString() doesn't escape, so this only
   * works providing the keys of the Counter do not have commas or equals signs
   * in them.
   *
   * @param s A String representation of a Counter
   * @return The Counter
   */
  public static ClassicCounter<String> fromString(String s) {
    ClassicCounter<String> result = new ClassicCounter<>();
    if (!s.startsWith("{") || !s.endsWith("}")) {
      throw new RuntimeException("invalid format: ||"+s+"||");
    }
    s = s.substring(1, s.length()-1);
    String[] lines = s.split(", ");
    for (String line : lines) {
      String[] fields = line.split("=");
      if (fields.length!=2) throw new RuntimeException("Got unsplittable line: \"" + line + '\"');
      result.setCount(fields[0], Double.parseDouble(fields[1]));
    }
    return result;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void prettyLog(RedwoodChannels channels, String description) {
    PrettyLogger.log(channels, description, Counters.asMap(this));
  }

View on GitHub (pinned to 1b7edd19c4)