chinabugotech/hutool · error · NullPointerException
PrimitiveConverter not allow null target type!
Error message
PrimitiveConverter not allow null target type!
What it means
PrimitiveConverter's constructor requires a non-null Class that is also a primitive type (byte/short/int/long/float/double/char/boolean). Passing null throws NullPointerException immediately; passing a non-primitive class throws IllegalArgumentException.
Source
Thrown at hutool-core/src/main/java/cn/hutool/core/convert/impl/PrimitiveConverter.java:40
* <li>{@code boolean}</li>
* </ul>
*
* @author Looly
*/
public class PrimitiveConverter extends AbstractConverter<Object> {
private static final long serialVersionUID = 1L;
private final Class<?> targetType;
/**
* 构造<br>
*
* @param clazz 需要转换的原始
* @throws IllegalArgumentException 传入的转换类型非原始类型时抛出
*/
public PrimitiveConverter(Class<?> clazz) {
if (null == clazz) {
throw new NullPointerException("PrimitiveConverter not allow null target type!");
} else if (false == clazz.isPrimitive()) {
throw new IllegalArgumentException("[" + clazz + "] is not a primitive class!");
}
this.targetType = clazz;
}
@Override
protected Object convertInternal(Object value) {
return PrimitiveConverter.convert(value, this.targetType, this::convertToStr);
}
@Override
protected String convertToStr(Object value) {
return StrUtil.trim(super.convertToStr(value));
}
@Override
@SuppressWarnings("unchecked")View on GitHub (pinned to 8870454b2a)
Solutions
- Null-check the class before constructing: Objects.requireNonNull(clazz).
- Route through Convert / ConverterRegistry which resolves primitive vs boxed types internally.
- Pass one of the primitive class literals (int.class, long.class, etc.) explicitly.
Example fix
// before
Converter c = new PrimitiveConverter(resolvedClass); // resolvedClass may be null
// after - guard then construct
if (resolvedClass == null || !resolvedClass.isPrimitive()) {
throw new IllegalArgumentException("primitive class required");
}
Converter c = new PrimitiveConverter(resolvedClass); Defensive patterns
Strategy: validation
Validate before calling
Objects.requireNonNull(clazz, "primitive class must not be null");
if (!clazz.isPrimitive()) {
throw new IllegalArgumentException("not a primitive class: " + clazz);
}
new PrimitiveConverter(clazz); Type guard
clazz != null && clazz.isPrimitive()
Try / catch
try {
return new PrimitiveConverter(clazz);
} catch (NullPointerException e) {
if (e.getMessage().contains("not allow null target type")) { /* resolve class first */ }
else throw e;
} Prevention
- Null-check and isPrimitive-check the class before constructing PrimitiveConverter.
- Prefer Convert / ConverterRegistry, which resolve primitive vs boxed types internally.
When it happens
Trigger: Constructing new PrimitiveConverter(null), typically when the class argument was derived reflectively and resolved to null (e.g. a generic type parameter with no erasure).
Common situations: Programmatic converter construction from reflected/generic types where the resolved class can be null; copy-paste omitting the class argument.
Related errors
- [type] and [defaultValue] are both null for Converter [{}],
- Default value [{}]({}) is not the instance of [{}]
- Video URI is empty
- File not found!
- The object to build a hash code for must not be null
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/c257b5cb439d0e74.
Report an issue: GitHub.