{"record":{"id":"4fae935cbe505c3e","repo":"spring-projects/spring-framework","slug":"targetlistclass-must-not-be-null","errorCode":null,"errorMessage":"'targetListClass' must not be null","messagePattern":"'targetListClass' must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/config/ListFactoryBean.java","lineNumber":61,"sourceCode":"\n\n\t/**\n\t * Set the source List, typically populated via XML \"list\" elements.\n\t */\n\tpublic void setSourceList(List<?> sourceList) {\n\t\tthis.sourceList = sourceList;\n\t}\n\n\t/**\n\t * Set the class to use for the target List. Can be populated with a fully\n\t * qualified class name when defined in a Spring application context.\n\t * <p>Default is a {@code java.util.ArrayList}.\n\t * @see java.util.ArrayList\n\t */\n\t@SuppressWarnings(\"rawtypes\")\n\tpublic void setTargetListClass(@Nullable Class<? extends List> targetListClass) {\n\t\tif (targetListClass == null) {\n\t\t\tthrow new IllegalArgumentException(\"'targetListClass' must not be null\");\n\t\t}\n\t\tif (!List.class.isAssignableFrom(targetListClass)) {\n\t\t\tthrow new IllegalArgumentException(\"'targetListClass' must implement [java.util.List]\");\n\t\t}\n\t\tthis.targetListClass = targetListClass;\n\t}\n\n\n\t@Override\n\t@SuppressWarnings(\"rawtypes\")\n\tpublic Class<List> getObjectType() {\n\t\treturn List.class;\n\t}\n\n\t@Override\n\t@SuppressWarnings(\"unchecked\")\n\tprotected List<Object> createInstance() {\n\t\tif (this.sourceList == 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/ListFactoryBean.java#L43-L79","documentation":"Thrown by ListFactoryBean.setTargetListClass() when it is explicitly invoked with null. The setter is annotated @Nullable yet guards against null because a null target list type is meaningless (ListFactoryBean defaults to java.util.ArrayList when the property is simply left unset). Spring's property binding calls the setter when the property is present in config, so this surfaces when a <property name=\"targetListClass\"/> element resolves to null.","triggerScenarios":"An XML <property name=\"targetListClass\"> that contains a null or empty value, or a parent/placeholder override that resolves the class name to null. Programmatic call factoryBean.setTargetListClass(null). Using a placeholder like ${list.type:} that resolves to an empty string bound to a Class argument.","commonSituations":"Overriding targetListClass via a properties/placeholder that is sometimes empty. Copy-pasting a ListFactoryBean bean definition and deleting the value but leaving the property element. Parent bean inheritance where the child clears the class to null.","solutions":["Remove the <property name=\"targetListClass\"/> element entirely; ListFactoryBean will default to ArrayList.","Provide a concrete List implementation, e.g. <property name=\"targetListClass\" value=\"java.util.LinkedList\"/>.","If the value comes from a placeholder, give it a non-null default, e.g. value=\"${list.type:java.util.ArrayList}\"."],"exampleFix":"// before (broken)\nListFactoryBean fb = new ListFactoryBean();\nfb.setSourceList(items);\nfb.setTargetListClass(null);\n// after\nListFactoryBean fb = new ListFactoryBean();\nfb.setSourceList(items);\n// simply omit setTargetListClass to use ArrayList","handlingStrategy":"validation","validationCode":"if (targetListClass == null) {\n    // omit the call entirely instead of passing null\n    return;\n}\nfactoryBean.setTargetListClass(targetListClass);","typeGuard":"static boolean isValidListClass(Class<?> c) {\n    return c != null && List.class.isAssignableFrom(c);\n}","tryCatchPattern":"try {\n    fb.setTargetListClass(clazz);\n} catch (IllegalArgumentException ex) {\n    // clazz was null: fall back to default ArrayList by skipping the setter\n    if (ex.getMessage().contains(\"must not be null\")) return;\n    throw ex;\n}","preventionTips":["Never call setTargetListClass(null); simply omit the call to use the ArrayList default.","In XML, remove the <property name=\"targetListClass\"/> element when you want the default.","Use a non-null placeholder default like ${list.type:java.util.ArrayList}."],"tags":["spring","factory-bean","configuration","collection","null-check"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}