{"record":{"id":"7802ab41bbd161fd","repo":"FasterXML/jackson-databind","slug":"argument-is-null","errorCode":null,"errorMessage":"argument \"{}\" is null","messagePattern":"argument \"(.+?)\" is null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/tools/jackson/databind/ObjectWriter.java","lineNumber":1301,"sourceCode":"    }\n\n    /**\n     * Helper method that applies configured {@link GeneratorInitializer},\n     * if any, to the given generator and returns it.\n     *\n     * @since 3.2\n     */\n    protected JsonGenerator _initializeGenerator(JsonGenerator gen) {\n        GeneratorInitializer init = _config.getGeneratorInitializer();\n        if (init != null) {\n            init.initialize(_config, gen);\n        }\n        return gen;\n    }\n\n    protected final void _assertNotNull(String paramName, Object src) {\n        if (src == null){\n            throw new IllegalArgumentException(\"argument \\\"\" + paramName + \"\\\" is null\");\n        }\n    }\n\n    /*\n    /**********************************************************************\n    /* Helper classes for configuration\n    /**********************************************************************\n     */\n\n    /**\n     * As a minor optimization, we will make an effort to pre-fetch a serializer,\n     * or at least relevant <code>TypeSerializer</code>, if given enough\n     * information.\n     */\n    public final static class Prefetch\n        implements java.io.Serializable\n    {\n        @Serial","sourceCodeStart":1283,"sourceCodeEnd":1319,"githubUrl":"https://github.com/FasterXML/jackson-databind/blob/a50c7d2a1d57234ac4adf70dbd88ac90db6436e4/src/main/java/tools/jackson/databind/ObjectWriter.java#L1283-L1319","documentation":"ObjectWriter._assertNotNull() is the writer-side precondition guard invoked by all writeValueAsString/writeValue/asString/acceptJsonFormatVisitor entry points (29 call sites). It throws IllegalArgumentException naming the null parameter (\"target\", \"g\", \"type\", \"visitor\") instead of letting a NullPointerException surface inside the serialization pipeline. It exists to give a clear, actionable failure at the public API boundary.","triggerScenarios":"Calling writer.writeValueAsString(null), writer.writeValue(out, nullTarget), writer.withType((JavaType)null), or writer.acceptJsonFormatVisitor(null, schema). Common when the value to serialize came from a lookup that returned null, a service that returned Optional-empty unwrapped unsafely, or a generic pipeline that forwards nullable inputs.","commonSituations":"Serializing the result of a repository.findById().get() on an empty Optional; writing a domain object whose factory returned null for an 'empty' state; HTTP handler passing a null entity to the serializer; event/message payload that is null when the producer had nothing to send.","solutions":["Null-check the target before serializing: if (target == null) handle/return-empty/throw-domain.","Use Optional at the boundary and only serialize when present.","Enable nullability annotations + static analysis to catch the null flow at compile time.","If null is a valid 'no payload' case, serialize an explicit empty object or skip serialization rather than passing null."],"exampleFix":"// before\nOrder o = repo.findById(id).orElse(null);\nString json = writer.writeValueAsString(o); // throws if order missing\n// after\nOrder o = repo.findById(id)\n    .orElseThrow(() -> new NotFoundException(id));\nString json = writer.writeValueAsString(o);","handlingStrategy":"validation","validationCode":"Object target = ...;\nif (target == null) throw new IllegalArgumentException(\"target must not be null\");\nwriter.writeValueAsString(target);","typeGuard":"// non-null check is the guard","tryCatchPattern":"try {\n    return writer.writeValueAsString(target);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().endsWith(\"is null\")) {\n        // return empty/204 or domain default\n    }\n    throw e;\n}","preventionTips":["Null-check the entity before serializing; handle 'not found' as a domain case.","Use Optional.orElseThrow at the boundary so null never reaches the writer.","Annotate producers with @NonNull and run static analysis."],"tags":["object-writer","null-check","precondition","api-misuse"],"analyzedSha":"a50c7d2a1d57234ac4adf70dbd88ac90db6436e4","analyzedAt":"2026-08-06T20:31:51.404Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}