Grasscutters/Grasscutter · error · IllegalArgumentException

Invalid token passed to SparseSet initializer - (split leng

Error message

Invalid token passed to SparseSet initializer -  (split length )

What it means

SparseSet parses initializer tokens; each token is split on '-' (or similar) and only lengths 1 (single int) or 2 (range) are valid. A token producing 0 or 3+ parts after split throws this IllegalArgumentException naming the offending token and the split length.

Source

Thrown at src/main/java/emu/grasscutter/utils/objects/SparseSet.java:24

    private final List<Range> rangeEntries;
    private final Set<Integer> denseEntries;

    public SparseSet(String csv) {
        this.rangeEntries = new ArrayList<>();
        this.denseEntries = new TreeSet<>();

        for (String token : csv.replace("\n", "").replace(" ", "").split(",")) {
            String[] tokens = token.split("-");
            switch (tokens.length) {
                case 1:
                    this.denseEntries.add(Integer.parseInt(tokens[0]));
                    break;
                case 2:
                    this.rangeEntries.add(
                            new Range(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1])));
                    break;
                default:
                    throw new IllegalArgumentException(
                            "Invalid token passed to SparseSet initializer - "
                                    + token
                                    + " (split length "
                                    + tokens.length
                                    + ")");
            }
        }
    }

    public boolean contains(int i) {
        for (Range range : this.rangeEntries) {
            if (range.check(i)) {
                return true;
            }
        }
        return this.denseEntries.contains(i);
    }

View on GitHub (pinned to f373827a83)

Solutions

  1. Fix the offending token so each is either a single integer or an integer range 'min-max'
  2. Split comma-separated groups correctly: ranges only take two endpoints, enumerate extra values separately
  3. Trim whitespace and remove stray hyphens from the spec string before constructing
  4. Validate the spec with a regex like ^-?\\d+(-(-?\\d+))?(,.*)*$ per token before parsing

Example fix

// before
SparseSet s = new SparseSet("1-2-3,10");
// after
SparseSet s = new SparseSet("1-2,3,10");
Defensive patterns

Strategy: validation

Validate before calling

void validateSparseSetSpec(String spec) {
  for (String token : spec.split(",")) {
    String[] parts = token.split("-");
    if (parts.length == 0 || parts.length > 2)
      throw new IllegalArgumentException("Bad SparseSet token: " + token);
  }
}

Type guard

boolean isValidSparseSetToken(String token) {
  return token.matches("-?\\d+(-(-?\\d+))?\\s*");
}

Try / catch

try {
  SparseSet set = new SparseSet(spec);
} catch (IllegalArgumentException e) {
  logger.error("Bad SparseSet spec '{}': {}", spec, e.getMessage());
}

Prevention

When it happens

Trigger: Constructing a SparseSet from a string spec where a token contains extra separators, e.g. "1-2-3", "--5", or "10-20-30-40"; any whitespace/duplicate-hyphen artifacts in the set definition string.

Common situations: Hand-edited resource lists (e.g. quest/scene id sets) with typos; copy-pasted data with double dashes; automated generators emitting comma-joined triplets.

Understand the failure class

Related errors


AI-assisted analysis of Grasscutters/Grasscutter@f373827a83 (2026-09-03). Data as JSON: /api/errors/961aca7f61958e9e. Report an issue: GitHub.