{"record":{"id":"03d0a88b5e183784","repo":"chinabugotech/hutool","slug":"comparatorchains-must-contain-at-least-one-compara","errorCode":null,"errorMessage":"ComparatorChains must contain at least one Comparator","messagePattern":"ComparatorChains must contain at least one Comparator","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/comparator/ComparatorChain.java","lineNumber":352,"sourceCode":"\t/**\n\t * 被锁定时抛出异常\n\t *\n\t * @throws UnsupportedOperationException 被锁定抛出此异常\n\t */\n\tprivate void checkLocked() {\n\t\tif (lock == true) {\n\t\t\tthrow new UnsupportedOperationException(\"Comparator ordering cannot be changed after the first comparison is performed\");\n\t\t}\n\t}\n\n\t/**\n\t * 检查比较器链是否为空，为空抛出异常\n\t *\n\t * @throws UnsupportedOperationException 为空抛出此异常\n\t */\n\tprivate void checkChainIntegrity() {\n\t\tif (chain.size() == 0) {\n\t\t\tthrow new UnsupportedOperationException(\"ComparatorChains must contain at least one Comparator\");\n\t\t}\n\t}\n\t//------------------------------------------------------------------------------------------------------------------------------- Private method start\n}\n","sourceCodeStart":334,"sourceCodeEnd":357,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/comparator/ComparatorChain.java#L334-L357","documentation":"Thrown by ComparatorChain.compare() on the first invocation if the chain contains zero comparators. The no-arg constructor creates an empty chain, and checkChainIntegrity() is called before the first comparison to ensure at least one comparator exists. A ComparatorChain with no comparators cannot produce a meaningful comparison.","triggerScenarios":"Constructing a ComparatorChain with the no-arg constructor (new ComparatorChain()) and then using it in a sort or comparison without adding any comparators via addComparator(). The error surfaces on the first compare() call, not at construction time.","commonSituations":"Creating an empty ComparatorChain and forgetting to add comparators before use. Dynamically building a chain where the comparator-adding logic produces zero entries due to a filtering bug. Using a ComparatorChain as a field that was initialized but never populated.","solutions":["Always add at least one comparator before using the chain: chain.addComparator(...) or use a parameterized constructor.","Use the static factory ComparatorChain.of(comparator) which requires at least one comparator.","Check chain.size() > 0 before passing the chain to a sort or comparison operation."],"exampleFix":"// before\nComparatorChain<MyType> chain = new ComparatorChain<>();\n// forgot to add comparators\nlist.sort(chain); // throws on first compare\n\n// after\nComparatorChain<MyType> chain = ComparatorChain.of(\n    Comparator.comparing(MyType::getName)\n);\nlist.sort(chain);","handlingStrategy":"validation","validationCode":"if (chain.size() == 0) {\n    throw new IllegalStateException(\"ComparatorChain must have at least one comparator before use\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always add at least one comparator before using the chain.","Use ComparatorChain.of(comparator) factory which requires a comparator.","Check chain.size() > 0 before sorting.","Avoid the no-arg constructor unless you immediately add comparators."],"tags":["comparator","empty-chain","sort","initialization"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}