{"record":{"id":"961aca7f61958e9e","repo":"Grasscutters/Grasscutter","slug":"invalid-token-passed-to-sparseset-initializer","errorCode":null,"errorMessage":"Invalid token passed to SparseSet initializer -  (split length )","messagePattern":"Invalid token passed to SparseSet initializer -  \\(split length \\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/emu/grasscutter/utils/objects/SparseSet.java","lineNumber":24,"sourceCode":"    private final List<Range> rangeEntries;\n    private final Set<Integer> denseEntries;\n\n    public SparseSet(String csv) {\n        this.rangeEntries = new ArrayList<>();\n        this.denseEntries = new TreeSet<>();\n\n        for (String token : csv.replace(\"\\n\", \"\").replace(\" \", \"\").split(\",\")) {\n            String[] tokens = token.split(\"-\");\n            switch (tokens.length) {\n                case 1:\n                    this.denseEntries.add(Integer.parseInt(tokens[0]));\n                    break;\n                case 2:\n                    this.rangeEntries.add(\n                            new Range(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1])));\n                    break;\n                default:\n                    throw new IllegalArgumentException(\n                            \"Invalid token passed to SparseSet initializer - \"\n                                    + token\n                                    + \" (split length \"\n                                    + tokens.length\n                                    + \")\");\n            }\n        }\n    }\n\n    public boolean contains(int i) {\n        for (Range range : this.rangeEntries) {\n            if (range.check(i)) {\n                return true;\n            }\n        }\n        return this.denseEntries.contains(i);\n    }\n","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/Grasscutters/Grasscutter/blob/f373827a83d3e688fd3c6afb1e1c3759ca1d61c9/src/main/java/emu/grasscutter/utils/objects/SparseSet.java#L6-L42","documentation":"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.","triggerScenarios":"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.","commonSituations":"Hand-edited resource lists (e.g. quest/scene id sets) with typos; copy-pasted data with double dashes; automated generators emitting comma-joined triplets.","solutions":["Fix the offending token so each is either a single integer or an integer range 'min-max'","Split comma-separated groups correctly: ranges only take two endpoints, enumerate extra values separately","Trim whitespace and remove stray hyphens from the spec string before constructing","Validate the spec with a regex like ^-?\\\\d+(-(-?\\\\d+))?(,.*)*$ per token before parsing"],"exampleFix":"// before\nSparseSet s = new SparseSet(\"1-2-3,10\");\n// after\nSparseSet s = new SparseSet(\"1-2,3,10\");","handlingStrategy":"validation","validationCode":"void validateSparseSetSpec(String spec) {\n  for (String token : spec.split(\",\")) {\n    String[] parts = token.split(\"-\");\n    if (parts.length == 0 || parts.length > 2)\n      throw new IllegalArgumentException(\"Bad SparseSet token: \" + token);\n  }\n}","typeGuard":"boolean isValidSparseSetToken(String token) {\n  return token.matches(\"-?\\\\d+(-(-?\\\\d+))?\\\\s*\");\n}","tryCatchPattern":"try {\n  SparseSet set = new SparseSet(spec);\n} catch (IllegalArgumentException e) {\n  logger.error(\"Bad SparseSet spec '{}': {}\", spec, e.getMessage());\n}","preventionTips":["Write each token as a single int or a two-endpoint 'min-max' range","Trim whitespace and collapse duplicate hyphens in spec strings","Regex-validate the whole spec before constructing SparseSet","Generate specs programmatically instead of hand-editing long lists"],"tags":["parsing","illegal-argument","sparse-set","configuration"],"backgroundTag":"invalid-range-specifier","analyzedSha":"f373827a83d3e688fd3c6afb1e1c3759ca1d61c9","analyzedAt":"2026-09-03T22:43:49.407Z","contentChangedAt":"2026-09-03T22:43:49.407Z","schemaVersion":2},"datasetVersion":"2026-09-11T07:07:21.782Z"}