{"record":{"id":"42c52147c34b84f0","repo":"chinabugotech/hutool","slug":"default-value-is-not-the-instance-of","errorCode":null,"errorMessage":"Default value [{}]({}) is not the instance of [{}]","messagePattern":"Default value \\[(.+?)\\]\\((.+?)\\) is not the instance of \\[(.+?)\\]","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/convert/AbstractConverter.java","lineNumber":61,"sourceCode":"\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(\n\t\t\t\tStrUtil.format(\"Default value [{}]({}) is not the instance of [{}]\", defaultValue, defaultValue.getClass(), targetType));\n\t\t}\n\t}\n\n\t/**\n\t * 内部转换器，被 {@link AbstractConverter#convert(Object, Object)} 调用，实现基本转换逻辑<br>\n\t * 内部转换器转换后如果转换失败可以做如下操作，处理结果都为返回默认值：\n\t *\n\t * <pre>\n\t * 1、返回{@code null}\n\t * 2、抛出一个{@link RuntimeException}异常\n\t * </pre>\n\t *\n\t * @param value 值\n\t * @return 转换后的类型\n\t */\n\tprotected abstract T convertInternal(Object value);\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/convert/AbstractConverter.java#L43-L79","documentation":"AbstractConverter.convert requires a non-null defaultValue to be an instance of the converter's targetType. If defaultValue is not null and not targetType.isInstance(defaultValue), it throws IllegalArgumentException.","triggerScenarios":"Calling an Integer converter with a String default, or any converter with a default whose runtime class is not assignable to the declared target type.","commonSituations":"Copy-paste of default values across converters of different types; generic helper passing an Object default into a typed converter.","solutions":["Supply a defaultValue whose type matches (or is a subtype of) targetType.","Pass null as the default if you do not need a fallback.","Use convertQuietly(value, defaultValue) which swallows the exception and returns the default on failure."],"exampleFix":"// before - default type mismatch\nInteger i = intConverter.convert(value, \"0\");\n\n// after - matching default\nInteger i = intConverter.convert(value, 0);","handlingStrategy":"type-guard","validationCode":"Class<T> tt = converter.getTargetType();\nif (defaultValue != null && tt != null && !tt.isInstance(defaultValue)) {\n    throw new IllegalArgumentException(\"default \" + defaultValue + \" not a \" + tt);\n}\nconverter.convert(value, defaultValue);","typeGuard":"default != null && converter.getTargetType() != null && converter.getTargetType().isInstance(defaultValue)","tryCatchPattern":"try {\n    converter.convert(value, defaultValue);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"not the instance of\")) { /* fix default type */ }\n    else throw e;\n}","preventionTips":["Make the default value's type match the converter's target type.","Pass null default when no fallback is needed; use convertQuietly for safe fallback."],"tags":["convert","type-mismatch","precondition"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}