quarkusio/quarkus · error · IllegalArgumentException

mediaType must not be null

Error message

mediaType must not be null

What it means

EntityPart.Builder.mediaType(MediaType) sets the MIME type of the multipart part and rejects null because a part must have a well-defined media type for serialization. Passing a null MediaType throws IllegalArgumentException immediately.

Source

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

    private Object content;
    private Class<?> contentType;
    private Type contentGenericType;

    public EntityPartBuilderImpl(String name, Serialisers serialisers) {
        if (name == null) {
            throw new IllegalArgumentException("name must not be null");
        }
        this.name = name;
        this.serialisers = serialisers;
        this.mediaType = MediaType.APPLICATION_OCTET_STREAM_TYPE;
        this.headers = new QuarkusMultivaluedHashMap<>();
    }

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

    @Override
    public EntityPart.Builder mediaType(String mediaTypeString) throws IllegalArgumentException {
        if (mediaTypeString == null) {
            throw new IllegalArgumentException("mediaType must not be null");
        }
        this.mediaType = MediaType.valueOf(mediaTypeString);
        return this;
    }

    @Override
    public EntityPart.Builder header(String headerName, String... headerValues) throws IllegalArgumentException {
        if (headerName == null) {
            throw new IllegalArgumentException("headerName must not be null");

View on GitHub (pinned to e1c734241f)

Solutions

  1. Ensure a concrete MediaType is passed, e.g. MediaType.APPLICATION_OCTET_STREAM_TYPE as a safe default.
  2. Validate/parse the content type string before constructing the MediaType and handle unknown types with a fallback.
  3. Use the String overload mediaType("application/pdf") with a non-null literal instead of pre-parsing.
  4. Fix the mapping (e.g. file-extension-to-MIME table) so it never returns null for expected inputs.

Example fix

// before
MediaType mt = mimeMap.get(extension); // may be null
builder.mediaType(mt);

// after
MediaType mt = mimeMap.getOrDefault(extension, MediaType.APPLICATION_OCTET_STREAM_TYPE);
builder.mediaType(mt);
Defensive patterns

Strategy: validation

Validate before calling

MediaType mt = resolveMediaType(extension);
Objects.requireNonNull(mt, "media type must not be null");
builder.mediaType(mt);

Type guard

MediaType requireMediaType(MediaType mt) {
    return mt != null ? mt : MediaType.APPLICATION_OCTET_STREAM_TYPE;
}

Try / catch

try {
    builder.mediaType(mediaType);
} catch (IllegalArgumentException e) {
    builder.mediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE);
}

Prevention

When it happens

Trigger: Calling partBuilder.mediaType((MediaType) null), typically when the MediaType was obtained from a lookup/parse that returned null.

Common situations: Deriving the media type from a file extension map or configuration that lacks an entry, leaving the MediaType null; a helper method returning null on unknown content types.

Related errors


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