{"record":{"id":"c257b5cb439d0e74","repo":"chinabugotech/hutool","slug":"primitiveconverter-not-allow-null-target-type","errorCode":null,"errorMessage":"PrimitiveConverter not allow null target type!","messagePattern":"PrimitiveConverter not allow null target type!","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/convert/impl/PrimitiveConverter.java","lineNumber":40,"sourceCode":" * \t\t<li>{@code boolean}</li>\n * </ul>\n *\n * @author Looly\n */\npublic class PrimitiveConverter extends AbstractConverter<Object> {\n\tprivate static final long serialVersionUID = 1L;\n\n\tprivate final Class<?> targetType;\n\n\t/**\n\t * 构造<br>\n\t *\n\t * @param clazz 需要转换的原始\n\t * @throws IllegalArgumentException 传入的转换类型非原始类型时抛出\n\t */\n\tpublic PrimitiveConverter(Class<?> clazz) {\n\t\tif (null == clazz) {\n\t\t\tthrow new NullPointerException(\"PrimitiveConverter not allow null target type!\");\n\t\t} else if (false == clazz.isPrimitive()) {\n\t\t\tthrow new IllegalArgumentException(\"[\" + clazz + \"] is not a primitive class!\");\n\t\t}\n\t\tthis.targetType = clazz;\n\t}\n\n\t@Override\n\tprotected Object convertInternal(Object value) {\n\t\treturn PrimitiveConverter.convert(value, this.targetType, this::convertToStr);\n\t}\n\n\t@Override\n\tprotected String convertToStr(Object value) {\n\t\treturn StrUtil.trim(super.convertToStr(value));\n\t}\n\n\t@Override\n\t@SuppressWarnings(\"unchecked\")","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/convert/impl/PrimitiveConverter.java#L22-L58","documentation":"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.","triggerScenarios":"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).","commonSituations":"Programmatic converter construction from reflected/generic types where the resolved class can be null; copy-paste omitting the class argument.","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."],"exampleFix":"// before\nConverter c = new PrimitiveConverter(resolvedClass); // resolvedClass may be null\n\n// after - guard then construct\nif (resolvedClass == null || !resolvedClass.isPrimitive()) {\n    throw new IllegalArgumentException(\"primitive class required\");\n}\nConverter c = new PrimitiveConverter(resolvedClass);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(clazz, \"primitive class must not be null\");\nif (!clazz.isPrimitive()) {\n    throw new IllegalArgumentException(\"not a primitive class: \" + clazz);\n}\nnew PrimitiveConverter(clazz);","typeGuard":"clazz != null && clazz.isPrimitive()","tryCatchPattern":"try {\n    return new PrimitiveConverter(clazz);\n} catch (NullPointerException e) {\n    if (e.getMessage().contains(\"not allow null target type\")) { /* resolve class first */ }\n    else throw e;\n}","preventionTips":["Null-check and isPrimitive-check the class before constructing PrimitiveConverter.","Prefer Convert / ConverterRegistry, which resolve primitive vs boxed types internally."],"tags":["convert","primitive","null-check","precondition"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}