FasterXML/jackson-databind · error · IllegalArgumentException

argument "{}" is null

Error message

argument "{}" is null

What it means

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.

Source

Thrown at src/main/java/tools/jackson/databind/ObjectWriter.java:1301

    }

    /**
     * Helper method that applies configured {@link GeneratorInitializer},
     * if any, to the given generator and returns it.
     *
     * @since 3.2
     */
    protected JsonGenerator _initializeGenerator(JsonGenerator gen) {
        GeneratorInitializer init = _config.getGeneratorInitializer();
        if (init != null) {
            init.initialize(_config, gen);
        }
        return gen;
    }

    protected final void _assertNotNull(String paramName, Object src) {
        if (src == null){
            throw new IllegalArgumentException("argument \"" + paramName + "\" is null");
        }
    }

    /*
    /**********************************************************************
    /* Helper classes for configuration
    /**********************************************************************
     */

    /**
     * As a minor optimization, we will make an effort to pre-fetch a serializer,
     * or at least relevant <code>TypeSerializer</code>, if given enough
     * information.
     */
    public final static class Prefetch
        implements java.io.Serializable
    {
        @Serial

View on GitHub (pinned to a50c7d2a1d)

Solutions

  1. Null-check the target before serializing: if (target == null) handle/return-empty/throw-domain.
  2. Use Optional at the boundary and only serialize when present.
  3. Enable nullability annotations + static analysis to catch the null flow at compile time.
  4. If null is a valid 'no payload' case, serialize an explicit empty object or skip serialization rather than passing null.

Example fix

// before
Order o = repo.findById(id).orElse(null);
String json = writer.writeValueAsString(o); // throws if order missing
// after
Order o = repo.findById(id)
    .orElseThrow(() -> new NotFoundException(id));
String json = writer.writeValueAsString(o);
Defensive patterns

Strategy: validation

Validate before calling

Object target = ...;
if (target == null) throw new IllegalArgumentException("target must not be null");
writer.writeValueAsString(target);

Type guard

// non-null check is the guard

Try / catch

try {
    return writer.writeValueAsString(target);
} catch (IllegalArgumentException e) {
    if (e.getMessage().endsWith("is null")) {
        // return empty/204 or domain default
    }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of FasterXML/jackson-databind@a50c7d2a1d (2026-08-06). Data as JSON: /api/errors/7802ab41bbd161fd. Report an issue: GitHub.