stanfordnlp/CoreNLP · error · IllegalArgumentException

Invalid nextBranchIndex=

Error message

Invalid nextBranchIndex=

What it means

getBranchId() in SequenceMatcher's branch-tracking state validates that the next branch index is within [1, nextTotal]; otherwise it throws IllegalArgumentException 'Invalid nextBranchIndex=..., nextTotal=...'. This is an internal invariant protecting the branch index bookkeeping used for backtracking alternatives.

Solutions

  1. Report/fix the pattern expression that produces an out-of-range nextBranchIndex
  2. Avoid reusing or sharing a SequenceMatcher across threads; create a fresh matcher per search
  3. Reset matcher state (via region/matcher re-creation) before reusing it for a new search
  4. Catch IllegalArgumentException as a signal of internal-state corruption and rebuild the matcher

Example fix

// before
// matcher reused across threads -> corrupted branch state
SequenceMatcher<T> m = sharedMatcher;
m.find();
// after
SequenceMatcher<T> m = pattern.matcher(elements);
m.find();
Defensive patterns

Strategy: try-catch

Try / catch

try {
  matcher.find();
} catch (IllegalArgumentException e) {
  if (e.getMessage().startsWith("Invalid nextBranchIndex=")) {
    log.error("Corrupted matcher branch state; rebuilding matcher", e);
    matcher = pattern.matcher(elements);
  }
}

Prevention

When it happens

Trigger: Reached when the matcher's internal branch state is inconsistent — e.g. nextBranchIndex computed as 0 or exceeding nextTotal during match enumeration with branching patterns — typically via getBranchId calls from matching internals.

Common situations: Rare in normal use; arises from bugs in custom pattern expression implementations, corrupted matcher state after concurrent or reentrant use, or deserialized matchers with stale branch indexes.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/SequenceMatcher.java:1182

      } else {
        return null;
      }
    }

    protected void setMatchedResult(int bid, int index, Object obj) {
      if (index >= 0) {
        Map<Integer,Object> matchedResults = getMatchedResults(bid, true);
        Object oldObj = matchedResults.get(index);
        if (oldObj != null) {
          logger.warning("Setting matchedResult=" + obj + ": index " + index + " already exists: " + oldObj);
        }
        matchedResults.put(index, obj);
      }
    }

    protected int getBranchId(int bid, int nextBranchIndex, int nextTotal) {
      if (nextBranchIndex <= 0 || nextBranchIndex > nextTotal) {
        throw new IllegalArgumentException("Invalid nextBranchIndex=" + nextBranchIndex + ", nextTotal=" + nextTotal);
      }
      if (nextTotal == 1) {
        return bid;
      } else {
        Pair<Integer,Integer> p = new Pair<>(bid, nextBranchIndex);
        int i = bidIndex.indexOf(p);
        if (i < 0) {
          for (int j = 0; j < nextTotal; j++) {
            bidIndex.add(new Pair<>(bid, j + 1));
          }
          i = bidIndex.indexOf(p);
        }
        return i;
      }
    }

    protected Map<SequencePattern.State,Object> getMatchStateInfo(int bid, boolean add) {
      BranchState bs = getBranchState(bid, add);

View on GitHub (pinned to 1b7edd19c4)