{"record":{"id":"0a8f04f7b39c1d33","repo":"quarkusio/quarkus","slug":"cannot-add-header-key-and-value-must-not-be-null","errorCode":null,"errorMessage":"Cannot add header, key and value must not be null","messagePattern":"Cannot add header, key and value must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"extensions/mailer/runtime/src/main/java/io/quarkus/mailer/Mail.java","lineNumber":318,"sourceCode":"    }\n\n    /**\n     * @return the current set of headers.\n     */\n    public Map<String, List<String>> getHeaders() {\n        return headers;\n    }\n\n    /**\n     * Adds a header value. If this header already has a value, the value is appended.\n     *\n     * @param key the header name, must not be {@code null}\n     * @param values the header values, must not be {@code null}\n     * @return the current {@link Mail}\n     */\n    public Mail addHeader(String key, String... values) {\n        if (key == null || values == null) {\n            throw new IllegalArgumentException(\"Cannot add header, key and value must not be null\");\n        }\n        List<String> content = this.headers.computeIfAbsent(key, k -> new ArrayList<>());\n        Collections.addAll(content, values);\n        return this;\n    }\n\n    /**\n     * Removes a header.\n     *\n     * @param key the header name, must not be {@code null}.\n     * @return the current {@link Mail}\n     */\n    public Mail removeHeader(String key) {\n        if (key == null) {\n            throw new IllegalArgumentException(\"Cannot remove header, key must not be null\");\n        }\n        headers.remove(key);\n        return this;","sourceCodeStart":300,"sourceCodeEnd":336,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/mailer/runtime/src/main/java/io/quarkus/mailer/Mail.java#L300-L336","documentation":"Mail.addHeader(key, values...) validates its arguments and throws IllegalArgumentException when the header name or the values array itself is null. Individual null entries inside the values array are allowed and stored as-is; the guard is only against a null name or a null array.","triggerScenarios":"Calling mail.addHeader(null, \"v\") or mail.addHeader(\"X-Custom\", (String[]) null) — typically when the header name/value comes from a variable or map lookup that returned null.","commonSituations":"Building headers from a Map<String,String> where a key/value is absent; dynamic header names from config or request data; null returned by a helper producing the header name.","solutions":["Ensure the header name is non-null before calling addHeader (check map/config lookup results)","Pass an empty array or skip the call when there are no values, instead of passing null","Validate upstream data that produces the header name"],"exampleFix":"// before\nString name = headerMap.get(\"Reply-To\");\nmail.addHeader(name, value); // NPE risk -> IAE when name==null\n// after\nif (name != null && value != null) {\n    mail.addHeader(name, value);\n}","handlingStrategy":"validation","validationCode":"if (key == null || values == null) {\n    throw new IllegalArgumentException(\"header name and values must not be null\");\n}\nmail.addHeader(key, values);","typeGuard":"boolean isAddableHeader(String key, String... values) {\n    return key != null && values != null;\n}","tryCatchPattern":"try {\n    mail.addHeader(key, values);\n} catch (IllegalArgumentException e) {\n    log.warnf(\"Skipped mail header: %s\", e.getMessage());\n}","preventionTips":["Null-check names/values sourced from maps, config, or user input before addHeader","Wrap dynamic header addition in a small helper that filters nulls","Prefer Map<String,List<String>> iteration with entry filtering over ad-hoc calls"],"tags":["quarkus","mailer","validation","null-check"],"backgroundTag":"null-argument-rejected","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}