{"record":{"id":"23c71acc08da92c3","repo":"chinabugotech/hutool","slug":"type-and-defaultvalue-are-both-null-for-conver","errorCode":null,"errorMessage":"[type] and [defaultValue] are both null for Converter [{}], we can not know what type to convert !","messagePattern":"\\[type\\] and \\[defaultValue\\] are both null for Converter \\[(.+?)\\], we can not know what type to convert !","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/convert/AbstractConverter.java","lineNumber":43,"sourceCode":"\t * @param value        被转换的值\n\t * @param defaultValue 默认值\n\t * @return 转换后的值\n\t * @since 4.5.7\n\t */\n\tpublic T convertQuietly(Object value, T defaultValue) {\n\t\ttry {\n\t\t\treturn convert(value, defaultValue);\n\t\t} catch (Exception e) {\n\t\t\treturn defaultValue;\n\t\t}\n\t}\n\n\t@Override\n\t@SuppressWarnings(\"unchecked\")\n\tpublic T convert(Object value, T defaultValue) {\n\t\tClass<T> targetType = getTargetType();\n\t\tif (null == targetType && null == defaultValue) {\n\t\t\tthrow new NullPointerException(StrUtil.format(\"[type] and [defaultValue] are both null for Converter [{}], we can not know what type to convert !\", this.getClass().getName()));\n\t\t}\n\t\tif (null == targetType) {\n\t\t\t// 目标类型不确定时使用默认值的类型\n\t\t\ttargetType = (Class<T>) defaultValue.getClass();\n\t\t}\n\t\tif (null == value) {\n\t\t\treturn defaultValue;\n\t\t}\n\n\t\tif (null == defaultValue || targetType.isInstance(defaultValue)) {\n\t\t\tif (targetType.isInstance(value) && false == Map.class.isAssignableFrom(targetType)) {\n\t\t\t\t// 除Map外，已经是目标类型，不需要转换（Map类型涉及参数类型，需要单独转换）\n\t\t\t\treturn targetType.cast(value);\n\t\t\t}\n\t\t\tfinal T result = convertInternal(value);\n\t\t\treturn ((null == result) ? defaultValue : result);\n\t\t} else {\n\t\t\tthrow new IllegalArgumentException(","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/convert/AbstractConverter.java#L25-L61","documentation":"AbstractConverter.convert(Object, T) resolves the target type from getTargetType(); if that returns null it falls back to defaultValue.getClass(). When BOTH are null it cannot decide the target type and throws NullPointerException.","triggerScenarios":"Calling converter.convert(value, null) on a converter whose getTargetType() returns null (e.g. a custom AbstractConverter subclass that never set its targetType field, or a misused CastConverter).","commonSituations":"Custom Converter implementations that override getTargetType() to return null; generic utility code that passes null defaults through.","solutions":["Ensure your converter's getTargetType() returns a non-null Class.","Pass a non-null defaultValue so its class can be used as the target type.","Avoid calling convert(value, null) directly; go through Convert / ConverterRegistry which always binds a target type."],"exampleFix":"// before\nconverter.convert(value, null); // getTargetType() returns null\n\n// after - pass a typed default\nInteger result = converter.convert(value, Integer.valueOf(0));","handlingStrategy":"validation","validationCode":"if (converter.getTargetType() == null && defaultValue == null) {\n    throw new IllegalStateException(\"Converter has no target type and no default: \" + converter);\n}\nconverter.convert(value, defaultValue);","typeGuard":null,"tryCatchPattern":"try {\n    converter.convert(value, null);\n} catch (NullPointerException e) {\n    if (e.getMessage().contains(\"both null\")) { /* supply default or skip */ }\n    else throw e;\n}","preventionTips":["Implement getTargetType() on every custom AbstractConverter subclass.","Route conversions through Convert / ConverterRegistry rather than calling raw converters with null defaults."],"tags":["convert","null-check","precondition"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}