apache/dolphinscheduler · error · IllegalArgumentException

Class type cannot be null

Error message

Class type cannot be null

What it means

JSONUtils.parseObject(String, Class<T>) deserializes a JSON string into an instance of the given class. It throws this IllegalArgumentException when the clazz argument is null — the target type is required to select the deserializer, so a null type is an immediate programming error, independent of the JSON content (an empty/null JSON string would otherwise just return null).

Source

Thrown at dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java:139

    }

    /**
     * This method deserializes the specified Json into an object of the specified class. It is not
     * suitable to use if the specified class is a generic type since it will not have the generic
     * type information because of the Type Erasure feature of Java. Therefore, this method should not
     * be used if the desired type is a generic type. Note that this method works fine if the any of
     * the fields of the specified object are generics, just the object itself should not be a
     * generic type.
     *
     * @param json  the string from which the object is to be deserialized
     * @param clazz the class of T
     * @param <T>   T
     * @return an object of type T from the string
     * classOfT
     */
    public static @Nullable <T> T parseObject(String json, Class<T> clazz) {
        if (clazz == null) {
            throw new IllegalArgumentException("Class type cannot be null");
        }

        if (Strings.isNullOrEmpty(json)) {
            return null;
        }

        try {
            return objectMapper.readValue(json, clazz);
        } catch (Exception e) {
            throw new IllegalArgumentException("Parse json: " + json + " to class: " + clazz.getName() + " failed", e);
        }
    }

    /**
     * deserialize
     *
     * @param src   byte array
     * @param clazz class

View on GitHub (pinned to 02eac45a1b)

Solutions

  1. Pass a concrete class literal: parseObject(json, MyDto.class) instead of a variable that can be null.
  2. If the class is looked up dynamically, check it before calling: if (clazz == null) throw new IllegalStateException("Unknown type: " + typeName);
  3. Fix the registry/enum lookup so every expected key maps to a class, or handle the miss before deserializing.
  4. Catch IllegalArgumentException at boundaries where dynamic types are expected to be occasionally unknown, and return a typed error result.

Example fix

// before
Class<?> clazz = TYPE_REGISTRY.get(typeName); // may be null
MyDto dto = JSONUtils.parseObject(json, clazz);
// after
Class<?> clazz = TYPE_REGISTRY.get(typeName);
if (clazz == null) {
    throw new IllegalStateException("Unknown typeName: " + typeName);
}
MyDto dto = JSONUtils.parseObject(json, clazz.asSubclass(MyDto.class));
Defensive patterns

Strategy: type-guard

Validate before calling

Objects.requireNonNull(clazz, "Target class for JSONUtils.parseObject must not be null");

Type guard

if (clazz != null) {
    T value = JSONUtils.parseObject(json, clazz);
}

Try / catch

try {
    return JSONUtils.parseObject(json, clazz);
} catch (IllegalArgumentException e) {
    throw new IllegalStateException("No target class resolved for deserialization", e);
}

Prevention

When it happens

Trigger: Calling JSONUtils.parseObject(json, null) or passing a Class<T> variable that was resolved dynamically (reflection, generics erasure, map lookup) and came back null, e.g. parseObject(payload, CLASS_MAP.get(typeName)) with an unknown typeName.

Common situations: Generic deserialization helpers parameterized by a class fetched from a registry/enum that no longer contains the key; refactoring where a Class parameter was accidentally dropped or not propagated; building calls via reflection where getDeclaredClass returned null.

Related errors


AI-assisted analysis of apache/dolphinscheduler@02eac45a1b (2026-09-06). Data as JSON: /api/errors/375b08938744f139. Report an issue: GitHub.