quarkusio/quarkus · error · IllegalArgumentException

type must not be null

Error message

type must not be null

What it means

EntityPart.Builder.content(T, Class<? extends T>) requires an explicit Class used to select the matching MessageBodyWriter; the type argument must not be null or IllegalArgumentException is thrown. The content object itself is validated first.

Solutions

  1. Pass the concrete class explicitly, e.g. content(dto, MyDto.class).
  2. Derive the class safely from the entity: entity.getClass() (valid since entity must be non-null anyway).
  3. In generic helpers, capture the Class as a method parameter and validate it.
  4. Fix the caller that passes a null Class variable.

Example fix

// before
builder.content(entity, typeClass); // typeClass may be null

// after
builder.content(entity, entity.getClass());
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(type, "content type must not be null");
builder.content(entity, type);

Type guard

<T> Class<? extends T> typeOf(T entity) {
    @SuppressWarnings("unchecked")
    Class<? extends T> c = (Class<? extends T>) entity.getClass();
    return c;
}

Try / catch

try {
    builder.content(entity, type);
} catch (IllegalArgumentException e) {
    builder.content(entity, entity.getClass());
}

Prevention

When it happens

Trigger: Calling partBuilder.content(entity, null) — passing the entity without a concrete Class, e.g. when the type is derived from a nullable variable or generics erasure.

Common situations: Generic helper methods that capture the type from a parameter which is null; using getClass() on a null entity or forwarding an uninitialized Class variable; reflection-based builders.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/3a7cbe67df009696. Report an issue: GitHub.

Appendix: source

Thrown at independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/jaxrs/EntityPartBuilderImpl.java:111

    @Override
    public EntityPart.Builder content(InputStream content) throws IllegalArgumentException {
        if (content == null) {
            throw new IllegalArgumentException("content must not be null");
        }
        this.content = content;
        this.contentType = null;
        this.contentGenericType = null;
        return this;
    }

    @Override
    public <T> EntityPart.Builder content(T content, Class<? extends T> type) throws IllegalArgumentException {
        if (content == null) {
            throw new IllegalArgumentException("content must not be null");
        }
        if (type == null) {
            throw new IllegalArgumentException("type must not be null");
        }
        this.content = content;
        this.contentType = type;
        this.contentGenericType = type;
        return this;
    }

    @Override
    public <T> EntityPart.Builder content(T content, GenericType<T> type) throws IllegalArgumentException {
        if (content == null) {
            throw new IllegalArgumentException("content must not be null");
        }
        if (type == null) {
            throw new IllegalArgumentException("type must not be null");
        }
        this.content = content;
        this.contentType = type.getRawType();
        this.contentGenericType = type.getType();

View on GitHub (pinned to e1c734241f)