{"record":{"id":"cab74a04a5096a0e","repo":"alibaba/COLA","slug":"random-cannot-be-null","errorCode":null,"errorMessage":"random cannot be null.","messagePattern":"random 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":64,"sourceCode":"\n    /**\n     * 生成随机的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]);","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/alibaba/COLA/blob/352e1a867538d9cd40e1b11e4028fa652fc97557/cola-components/cola-component-job/src/main/java/com/alibaba/cola/job/UuidGenerator.java#L46-L82","documentation":"UuidGenerator.randomNanoId(Random, char[], int) validates its arguments and throws IllegalArgumentException when the supplied Random is null. This is a NanoId-style generator ported into cola-job; the null check is a standard precondition.","triggerScenarios":"Calling UuidGenerator.randomNanoId(null, alphabet, size) or indirectly passing a null Random from configuration or a factory.","commonSituations":"Passing an uninitialized SecureRandom field that was never assigned; a supplier of randomness returning null; copy-paste of the overload call with the wrong first argument.","solutions":["Pass a non-null Random, e.g. new SecureRandom()","Use the no-arg randomNanoId() convenience overload which uses the default generator","Fix the field/bean that should hold a Random instance but is null"],"exampleFix":"// before\nString id = UuidGenerator.randomNanoId(null, alphabet, 21);\n// after\nString id = UuidGenerator.randomNanoId(new SecureRandom(), alphabet, 21);","handlingStrategy":"validation","validationCode":"if (random == null) throw new IllegalArgumentException(\"supply a Random, e.g. new SecureRandom()\");","typeGuard":"String safeNanoId(Random random, char[] alphabet, int size) {\n    Random r = random != null ? random : new SecureRandom();\n    return UuidGenerator.randomNanoId(r, alphabet != null ? alphabet : DEFAULT_ALPHABET, Math.max(1, size));\n}","tryCatchPattern":"try {\n    id = UuidGenerator.randomNanoId(random, alphabet, size);\n} catch (IllegalArgumentException e) {\n    log.warn(\"randomNanoId invalid argument: {}\", e.getMessage());\n    id = UuidGenerator.randomNanoId();\n}","preventionTips":["Prefer the no-arg UuidGenerator.randomNanoId() unless custom randomness is required","Initialize Random/SecureRandom fields eagerly (final fields)","Null-check dependencies before delegating to generator utilities"],"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"}