{"record":{"id":"2875a6cb34dec8b2","repo":"alibaba/COLA","slug":"alphabet-cannot-be-null","errorCode":null,"errorMessage":"alphabet cannot be null.","messagePattern":"alphabet cannot be null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"cola-components/cola-component-job/src/main/java/com/alibaba/cola/job/UuidGenerator.java","lineNumber":66,"sourceCode":"     * 生成随机的NanoId工具内部类\n     */\n    public final class NanoIdUtils {\n        public static final SecureRandom DEFAULT_NUMBER_GENERATOR = new SecureRandom();\n        public static final char[] DEFAULT_ALPHABET = \"_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\".toCharArray();\n        public static final int DEFAULT_SIZE = 21;\n\n        private NanoIdUtils() {\n        }\n\n        public static String randomNanoId() {\n            return randomNanoId(DEFAULT_NUMBER_GENERATOR, DEFAULT_ALPHABET, 21);\n        }\n\n        public static String randomNanoId(Random random, char[] alphabet, int size) {\n            if (random == null) {\n                throw new IllegalArgumentException(\"random cannot be null.\");\n            } else if (alphabet == null) {\n                throw new IllegalArgumentException(\"alphabet cannot be null.\");\n            } else if (alphabet.length != 0 && alphabet.length < 256) {\n                if (size <= 0) {\n                    throw new IllegalArgumentException(\"size must be greater than zero.\");\n                } else {\n                    int mask = (2 << (int)Math.floor(Math.log((double)(alphabet.length - 1)) / Math.log(2.0D))) - 1;\n                    int step = (int)Math.ceil(1.6D * (double)mask * (double)size / (double)alphabet.length);\n                    StringBuilder idBuilder = new StringBuilder();\n\n                    while(true) {\n                        byte[] bytes = new byte[step];\n                        random.nextBytes(bytes);\n\n                        for(int i = 0; i < step; ++i) {\n                            int alphabetIndex = bytes[i] & mask;\n                            if (alphabetIndex < alphabet.length) {\n                                idBuilder.append(alphabet[alphabetIndex]);\n                                if (idBuilder.length() == size) {\n                                    return idBuilder.toString();","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/alibaba/COLA/blob/352e1a867538d9cd40e1b11e4028fa652fc97557/cola-components/cola-component-job/src/main/java/com/alibaba/cola/job/UuidGenerator.java#L48-L84","documentation":"Argument-validation guard inside NanoIdUtils.randomNanoId: the alphabet character array passed to the generator was null, so no character set exists to map random bytes onto. It fires only via the full overload randomNanoId(Random, char[], int) when a caller explicitly supplies a null alphabet; the no-arg convenience method always passes DEFAULT_ALPHABET and cannot trigger it.","triggerScenarios":"Calling randomNanoId(random, null, size) with a null alphabet array.","commonSituations":"Loading a custom alphabet from config that failed to initialize; passing a null result of a builder method; typo where DEFAULT_ALPHABET was replaced by null.","solutions":["Pass a non-null char[] alphabet, e.g. UuidGenerator.DEFAULT_ALPHABET or \"abc...\".toCharArray()","Fix the config/provider that returns a null alphabet","Use the no-arg randomNanoId() overload that supplies the default alphabet"],"exampleFix":"// before\nString id = gen.randomNanoId(random, null, 21);\n// after\nString id = gen.randomNanoId(random, DEFAULT_ALPHABET, 21);","handlingStrategy":"validation","validationCode":"if (alphabet == null || alphabet.length == 0) {\n    alphabet = UuidGenerator.DEFAULT_ALPHABET; // or handle explicitly\n}","typeGuard":"char[] safeAlphabet(char[] alphabet) {\n    return (alphabet != null && alphabet.length >= 1 && alphabet.length <= 255) ? alphabet : DEFAULT_ALPHABET;\n}","tryCatchPattern":"try {\n    id = UuidGenerator.randomNanoId(random, alphabet, size);\n} catch (IllegalArgumentException e) {\n    log.warn(\"alphabet invalid ({}); falling back to default\", e.getMessage());\n    id = UuidGenerator.randomNanoId();\n}","preventionTips":["Keep custom alphabets as constants, not nullable fields","Validate alphabet-loaded-from-config at startup","Use DEFAULT_ALPHABET when unsure"],"tags":["java","uuid","null-argument"],"backgroundTag":"null-argument","analyzedSha":"352e1a867538d9cd40e1b11e4028fa652fc97557","analyzedAt":"2026-09-08T00:46:23.972Z","contentChangedAt":"2026-09-08T00:46:23.972Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}