{"record":{"id":"6763c779f2f3fce7","repo":"spring-projects/spring-security","slug":"string-cannot-be-null","errorCode":null,"errorMessage":"String cannot be null","messagePattern":"String cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"crypto/src/main/java/org/springframework/security/crypto/codec/Utf8.java","lineNumber":47,"sourceCode":" * UTF-8 Charset encoder/decoder.\n * <p>\n * For internal use only.\n *\n * @author Luke Taylor\n */\npublic final class Utf8 {\n\n\tprivate static final Charset CHARSET = StandardCharsets.UTF_8;\n\n\tprivate Utf8() {\n\t}\n\n\t/**\n\t * Get the bytes of the String in UTF-8 encoded form.\n\t */\n\tpublic static byte[] encode(CharSequence string) {\n\t\tif (string == null) {\n\t\t\tthrow new IllegalArgumentException(\"String cannot be null\");\n\t\t}\n\t\ttry {\n\t\t\tByteBuffer bytes = CHARSET.newEncoder().encode(CharBuffer.wrap(string));\n\t\t\tbyte[] bytesCopy = new byte[bytes.limit()];\n\t\t\tSystem.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit());\n\t\t\treturn bytesCopy;\n\t\t}\n\t\tcatch (CharacterCodingException ex) {\n\t\t\tthrow new IllegalArgumentException(\"Encoding failed\", ex);\n\t\t}\n\t}\n\n\t/**\n\t * Decode the bytes in UTF-8 form into a String.\n\t */\n\tpublic static String decode(byte[] bytes) {\n\t\ttry {\n\t\t\treturn CHARSET.newDecoder().decode(ByteBuffer.wrap(bytes)).toString();","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/crypto/src/main/java/org/springframework/security/crypto/codec/Utf8.java#L29-L65","documentation":"Utf8.encode converts a CharSequence to UTF-8 bytes but explicitly rejects null input with an IllegalArgumentException. The charset encoder itself cannot be handed a null sequence, so the library checks up front and fails with a clear message.","triggerScenarios":"Calling Utf8.encode(null), commonly when the argument originates from an optional request parameter, a nullable DB column, or a config value that is absent.","commonSituations":"Encoding a password/secret field that is null because a property wasn't set; passing the result of map.get(\"key\") without a null check; NPE-avoidance refactors that route nulls here.","solutions":["Ensure the input CharSequence is non-null before calling encode.","Decide the null semantics: return empty bytes or skip encoding when input is null.","Throw your own descriptive exception or use Objects.requireNonNull with a message at the call site."],"exampleFix":"// before\nbyte[] bytes = Utf8.encode(config.getPassword()); // NPE-adjacent throw if null\n// after\nString pwd = config.getPassword();\nbyte[] bytes = (pwd == null) ? new byte[0] : Utf8.encode(pwd);","handlingStrategy":"validation","validationCode":"if (input == null) throw new IllegalArgumentException(\"cannot encode null string\");\nbyte[] bytes = Utf8.encode(input);","typeGuard":"boolean isNonNull(CharSequence s) { return s != null; }","tryCatchPattern":"try { bytes = Utf8.encode(s); } catch (IllegalArgumentException e) { bytes = new byte[0]; /* or rethrow with context */ }","preventionTips":["Apply Objects.requireNonNull(msg) at API boundaries to fail with context.","Resolve optional config values before passing to encode.","Establish null-handling conventions (empty string vs null) for secret/credential inputs."],"tags":["utf8","null-argument","spring-security","encoding"],"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"}