{"record":{"id":"084b942e7e421e57","repo":"stanfordnlp/CoreNLP","slug":"could-not-compute-span-from-tokens","errorCode":null,"errorMessage":"Could not compute span from tokens!","messagePattern":"Could not compute span from tokens!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/naturalli/Util.java","lineNumber":625,"sourceCode":"     add(\"past\");\n     add(\"proposed\");\n  }});\n\n  /**\n   * Construct the spanning span of the given list of tokens.\n   *\n   * @param tokens The tokens that should define the span.\n   * @return A span (0-indexed) that covers all of the tokens.\n   */\n  public static Span tokensToSpan(List<? extends HasIndex> tokens) {\n    int min = Integer.MAX_VALUE;\n    int max = Integer.MIN_VALUE;\n    for (HasIndex token : tokens) {\n      min = Math.min(token.index() - 1, min);\n      max = Math.max(token.index(), max);\n    }\n    if (min < 0 || max == Integer.MAX_VALUE) {\n      throw new IllegalArgumentException(\"Could not compute span from tokens!\");\n    } else if (min >= max) {\n      throw new IllegalStateException(\"Either logic is broken or Gabor can't code.\");\n    } else {\n      return new Span(min, max);\n    }\n  }\n}\n","sourceCodeStart":607,"sourceCodeEnd":633,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/naturalli/Util.java#L607-L633","documentation":"Util.tokensToSpan computes the token-index span covering a collection of CoreLabel/HasIndex tokens, initializing min to Integer.MAX_VALUE and max to Integer.MIN_VALUE. If no token yields a valid index (min stays at the sentinel or max stays at the sentinel), it throws IllegalArgumentException \"Could not compute span from tokens!\" — effectively meaning the token list is empty or all indices are degenerate.","triggerScenarios":"Calling Util.tokensToSpan with an empty token collection (min remains Integer.MAX_VALUE... note the check is min < 0 || max == Integer.MAX_VALUE) or with tokens having out-of-range indices, so min < 0 or max == Integer.MIN_VALUE path produces the sentinel condition.","commonSituations":"Passing an empty list of tokens from a failed sentence split; tokens with index 0 or unset indices from partially populated CoreLabels.","solutions":["Check that the token collection is non-empty and tokens have valid (>=1) sentence indices before calling tokensToSpan","Fix upstream sentence/token extraction so indices are populated","Catch IllegalArgumentException and return a default/empty span"],"exampleFix":"// before\nSpan s = Util.tokensToSpan(tokens);\n// after\nif (tokens.isEmpty()) return null;\nSpan s = Util.tokensToSpan(tokens);","handlingStrategy":"validation","validationCode":"if (tokens == null || tokens.isEmpty() || tokens.stream().anyMatch(t -> t.index() < 1)) return null; // skip tokensToSpan","typeGuard":"boolean hasValidTokens(List<? extends HasIndex> toks) { return toks != null && !toks.isEmpty() && toks.stream().allMatch(t -> t.index() >= 1); }","tryCatchPattern":"try { return Util.tokensToSpan(tokens); } catch (IllegalArgumentException e) { return null; }","preventionTips":["Ensure sentence splitting produced non-empty token lists","Check CoreLabel indices are populated before span conversion"],"tags":["java","illegal-argument","empty-collection"],"backgroundTag":"empty-required-field","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}