{"record":{"id":"da8bd89e9359df6d","repo":"spring-projects/spring-security","slug":"encode-cannot-be-null","errorCode":null,"errorMessage":"encode cannot be null","messagePattern":"encode cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"crypto/src/main/java/org/springframework/security/crypto/keygen/Base64StringKeyGenerator.java","lineNumber":69,"sourceCode":"\t\tthis(Base64.getEncoder(), keyLength);\n\t}\n\n\t/**\n\t * Creates an instance with keyLength of 32 bytes and the provided encoder.\n\t * @param encoder the encoder to use\n\t */\n\tpublic Base64StringKeyGenerator(Base64.Encoder encoder) {\n\t\tthis(encoder, DEFAULT_KEY_LENGTH);\n\t}\n\n\t/**\n\t * Creates an instance with the provided key length and encoder.\n\t * @param encoder the encoder to use\n\t * @param keyLength the key length to use\n\t */\n\tpublic Base64StringKeyGenerator(Base64.Encoder encoder, int keyLength) {\n\t\tif (encoder == null) {\n\t\t\tthrow new IllegalArgumentException(\"encode cannot be null\");\n\t\t}\n\t\tif (keyLength <= 0) {\n\t\t\tthrow new IllegalArgumentException(\"keyLength must be greater than 0\");\n\t\t}\n\t\tthis.encoder = encoder;\n\t\tthis.keyGenerator = KeyGenerators.secureRandom(keyLength);\n\t}\n\n\t@Override\n\tpublic String generateKey() {\n\t\tbyte[] key = this.keyGenerator.generateKey();\n\t\tbyte[] base64EncodedKey = this.encoder.encode(key);\n\t\treturn new String(base64EncodedKey);\n\t}\n\n}\n","sourceCodeStart":51,"sourceCodeEnd":86,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/crypto/src/main/java/org/springframework/security/crypto/keygen/Base64StringKeyGenerator.java#L51-L86","documentation":"The Base64StringKeyGenerator(Base64.Encoder, int) constructor validates that the supplied Base64.Encoder is non-null and throws IllegalArgumentException('encode cannot be null') otherwise. The encoder determines how the random key bytes are rendered as a string, so a null encoder is an invalid construction argument.","triggerScenarios":"Calling new Base64StringKeyGenerator(null, someLength) or passing an encoder variable that was never initialized (e.g. a conditional encoder expression that resolved to null).","commonSituations":"Building the encoder dynamically from config and forgetting a default; refactoring where Base64.getEncoder() call was dropped; DI/property binding yielding a null encoder object.","solutions":["Pass a concrete encoder such as Base64.getEncoder(), Base64.getUrlEncoder(), or Base64.getMimeEncoder().","If the encoder is configurable, apply Objects.requireNonNullElse(encoder, Base64.getEncoder()) before constructing.","Use the no-arg or single-int constructors if URL-safe Base64 defaults are acceptable."],"exampleFix":"// before\nBase64.Encoder enc = config.isUrlSafe() ? Base64.getUrlEncoder() : null;\nBase64StringKeyGenerator gen = new Base64StringKeyGenerator(enc, 32);\n// after\nBase64.Encoder enc = config.isUrlSafe() ? Base64.getUrlEncoder() : Base64.getEncoder();\nBase64StringKeyGenerator gen = new Base64StringKeyGenerator(enc, 32);","handlingStrategy":"validation","validationCode":"if (encoder == null) {\n    throw new IllegalArgumentException(\"encoder must be non-null: use Base64.getEncoder() or Base64.getUrlEncoder()\");\n}\nBase64StringKeyGenerator gen = new Base64StringKeyGenerator(encoder, keyLength);","typeGuard":"boolean isValidEncoder(java.util.Base64.Encoder e) { return e != null; }","tryCatchPattern":"try {\n    generator = new Base64StringKeyGenerator(encoder, keyLength);\n} catch (IllegalArgumentException ex) {\n    generator = new Base64StringKeyGenerator(Base64.getEncoder(), keyLength); // safe default\n}","preventionTips":["Always pass an explicit encoder: Base64.getEncoder(), getUrlEncoder(), or getMimeEncoder()","Never assign a potentially-null configurable encoder directly; default it first with Objects.requireNonNullElse","Prefer the no-arg Base64StringKeyGenerator() when defaults are acceptable","Add a unit test that constructs the generator with each configured encoder option"],"tags":["keygen","base64","null-argument","constructor"],"backgroundTag":"null-argument","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}