stanfordnlp/CoreNLP · error · IllegalArgumentException

Last character of encoded array is escape character: " +…

Error message

Last character of encoded array is escape character: " + encoded

What it means

During the finite-state scan of an encoded array, a backslash starts an escape sequence: the next character is taken literally. If the backslash is the very last character of the input there is nothing to escape, so the input is malformed and the decoder throws this IllegalArgumentException.

Solutions

  1. Remove the trailing backslash or supply the character it should escape
  2. Fix the encoder so escapes are always followed by the escaped character
  3. Check for double-escaping in Java string literals ("\\\\" vs "\\")
  4. Validate the string ends with a legal (non-escape) character before decoding

Example fix

// before
String encoded = "(a,b\\)"; // raw string ends with backslash
// after
String encoded = "(a,b\\\\)"; // escaped backslash, decodes to (a,b\)
Defensive patterns

Strategy: validation

Validate before calling

if (s != null && s.endsWith("\\")) throw new IllegalArgumentException("Encoded string must not end with a lone backslash");

Type guard

static boolean endsWithLoneEscape(String s) { return s != null && !s.isEmpty() && s.charAt(s.length()-1) == '\\'; }

Try / catch

try { terms = StringUtils.decode(encoded); } catch (IllegalArgumentException e) { if (e.getMessage().contains("escape character")) { /* repair or reject input */ } else throw e; }

Prevention

When it happens

Trigger: Passing an encoded array whose final character is a lone backslash, e.g. "(a,b\\)" in Java source where the raw string ends in '\\'; trailing escape produced by an escaping bug in the encoder.

Common situations: Double-escaping mistakes in Java string literals (miscounting backslashes); encoders that escape but drop the escaped char; truncation cutting the string right after a backslash.

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/2a8d968ef5c7e265. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/util/StringUtils.java:2582

    int start = 0; int end = chars.length;
    if(chars[0] == '('){ start += 1; end -= 1; if(chars[end] != ')') throw new IllegalArgumentException("Unclosed paren in encoded array: " + encoded); }
    if(chars[0] == '['){ start += 1; end -= 1; if(chars[end] != ']') throw new IllegalArgumentException("Unclosed bracket in encoded array: " + encoded); }
    if(chars[0] == '{'){ start += 1; end -= 1; if(chars[end] != '}') throw new IllegalArgumentException("Unclosed bracke in encoded array: " + encoded); }
    // (finite state automaton)
    for (int i=start; i<end; i++) {
      if (chars[i] == '\r') {
        // Ignore funny windows carriage return
        continue;
      } else if (quoteCloseChar != 0) {
        //(case: in quotes)
        if(chars[i] == quoteCloseChar){
          quoteCloseChar = (char) 0;
        }else{
          current.append(chars[i]);
        }
      } else if(chars[i] == '\\'){
        //(case: escaped character)
        if(i == chars.length - 1) throw new IllegalArgumentException("Last character of encoded array is escape character: " + encoded);
        current.append(chars[i+1]);
        i += 1;
      } else {
        //(case: normal)
        if (chars[i] == '"') {
          quoteCloseChar = '"';
        } else if(chars[i] == '\'') {
          quoteCloseChar = '\'';
        } else if(chars[i] == ',' || chars[i] == ';' || chars[i] == ' ' || chars[i] == '\t' || chars[i] == '\n') {
          //break
          if (current.length() > 0) {
            terms.add(current.toString().trim());
          }
          current = new StringBuilder();
        } else {
          current.append(chars[i]);
        }
      }

View on GitHub (pinned to 1b7edd19c4)