{"record":{"id":"317ec6ed7dc64d5d","repo":"spring-projects/spring-framework","slug":"targetmapclass-must-not-be-null","errorCode":null,"errorMessage":"'targetMapClass' must not be null","messagePattern":"'targetMapClass' must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/config/MapFactoryBean.java","lineNumber":61,"sourceCode":"\n\n\t/**\n\t * Set the source Map, typically populated via XML \"map\" elements.\n\t */\n\tpublic void setSourceMap(Map<?, ?> sourceMap) {\n\t\tthis.sourceMap = sourceMap;\n\t}\n\n\t/**\n\t * Set the class to use for the target Map. Can be populated with a fully\n\t * qualified class name when defined in a Spring application context.\n\t * <p>Default is a linked HashMap, keeping the registration order.\n\t * @see java.util.LinkedHashMap\n\t */\n\t@SuppressWarnings(\"rawtypes\")\n\tpublic void setTargetMapClass(@Nullable Class<? extends Map> targetMapClass) {\n\t\tif (targetMapClass == null) {\n\t\t\tthrow new IllegalArgumentException(\"'targetMapClass' must not be null\");\n\t\t}\n\t\tif (!Map.class.isAssignableFrom(targetMapClass)) {\n\t\t\tthrow new IllegalArgumentException(\"'targetMapClass' must implement [java.util.Map]\");\n\t\t}\n\t\tthis.targetMapClass = targetMapClass;\n\t}\n\n\n\t@Override\n\t@SuppressWarnings(\"rawtypes\")\n\tpublic Class<Map> getObjectType() {\n\t\treturn Map.class;\n\t}\n\n\t@Override\n\t@SuppressWarnings(\"unchecked\")\n\tprotected Map<Object, Object> createInstance() {\n\t\tif (this.sourceMap == null) {","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/config/MapFactoryBean.java#L43-L79","documentation":"Thrown by MapFactoryBean.setTargetMapClass() when explicitly passed null. Mirror of ListFactoryBean's guard: MapFactoryBean defaults to a LinkedHashMap when the property is unset, so an explicit null is a configuration mistake rather than a meaningful value. The setter is invoked by Spring's property binding whenever the property element is present.","triggerScenarios":"XML <property name=\"targetMapClass\"> resolving to null (empty element or placeholder that yields null). Programmatic call mapFactoryBean.setTargetMapClass(null). A parent-bean override that clears the value.","commonSituations":"Placeholder such as value=\"${map.type:}\" resolving to empty and bound to Class. Removing a targetMapClass value but leaving the property element. Bean inheritance clearing the property in a child.","solutions":["Delete the <property name=\"targetMapClass\"/> element to fall back to LinkedHashMap.","Supply a concrete Map type, e.g. value=\"java.util.concurrent.ConcurrentHashMap\".","Give the placeholder a non-null default: value=\"${map.type:java.util.LinkedHashMap}\"."],"exampleFix":"// before (broken)\nMapFactoryBean fb = new MapFactoryBean();\nfb.setSourceMap(map);\nfb.setTargetMapClass(null);\n// after\nMapFactoryBean fb = new MapFactoryBean();\nfb.setSourceMap(map);\n// omit setTargetMapClass for LinkedHashMap","handlingStrategy":"validation","validationCode":"if (targetMapClass == null) {\n    return; // omit the setter to use LinkedHashMap default\n}\nfactoryBean.setTargetMapClass(targetMapClass);","typeGuard":"static boolean isValidMapClass(Class<?> c) {\n    return c != null && Map.class.isAssignableFrom(c);\n}","tryCatchPattern":"try {\n    fb.setTargetMapClass(clazz);\n} catch (IllegalArgumentException ex) {\n    if (ex.getMessage().contains(\"must not be null\")) return;\n    throw ex;\n}","preventionTips":["Never call setTargetMapClass(null); omit the call to keep the LinkedHashMap default.","Provide a non-null placeholder default such as ${map.type:java.util.LinkedHashMap}.","In XML, delete the property element rather than blanking its value."],"tags":["spring","factory-bean","collection","null-check","configuration"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}