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 classView on GitHub (pinned to 02eac45a1b)
Solutions
- Pass a concrete class literal: parseObject(json, MyDto.class) instead of a variable that can be null.
- If the class is looked up dynamically, check it before calling: if (clazz == null) throw new IllegalStateException("Unknown type: " + typeName);
- Fix the registry/enum lookup so every expected key maps to a class, or handle the miss before deserializing.
- 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
- Pass class literals (Foo.class) instead of nullable Class variables whenever possible.
- Null-check results of dynamic class lookups (maps, reflection) immediately after the lookup.
- Keep type registries exhaustive: fail at registration/init time if a key has no class.
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
- Type reference cannot be null
- Object: + obj + to json serialization exception.
- itemsList is null
- headerParams is not a valid json
- bodyParams is not a valid json
AI-assisted analysis of apache/dolphinscheduler@02eac45a1b (2026-09-06).
Data as JSON: /api/errors/375b08938744f139.
Report an issue: GitHub.