{"record":{"id":"4a7fa4f3a0f7b2b1","repo":"mybatis/mybatis-3","slug":"cannot-set-the-value-because-the-property","errorCode":null,"errorMessage":"Cannot set the value '{}' because the property '{}' is null.","messagePattern":"Cannot set the value '(.+?)' because the property '(.+?)' is null\\.","errorType":"exception","errorClass":"ReflectionException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/reflection/wrapper/BaseWrapper.java","lineNumber":82,"sourceCode":"    } else if (collection instanceof double[]) {\n      return ((double[]) collection)[i];\n    } else if (collection instanceof float[]) {\n      return ((float[]) collection)[i];\n    } else if (collection instanceof int[]) {\n      return ((int[]) collection)[i];\n    } else if (collection instanceof long[]) {\n      return ((long[]) collection)[i];\n    } else if (collection instanceof short[]) {\n      return ((short[]) collection)[i];\n    } else {\n      throw new ReflectionException(\"Cannot get the value '\" + prop.getIndexedName() + \"' because the property '\"\n          + prop.getName() + \"' is not Map, List or Array.\");\n    }\n  }\n\n  protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {\n    if (collection == null) {\n      throw new ReflectionException(\"Cannot set the value '\" + prop.getIndexedName() + \"' because the property '\"\n          + prop.getName() + \"' is null.\");\n    }\n    if (collection instanceof Map) {\n      ((Map) collection).put(prop.getIndex(), value);\n    } else {\n      int i = Integer.parseInt(prop.getIndex());\n      if (collection instanceof List) {\n        ((List) collection).set(i, value);\n      } else if (collection instanceof Object[]) {\n        ((Object[]) collection)[i] = value;\n      } else if (collection instanceof char[]) {\n        ((char[]) collection)[i] = (Character) value;\n      } else if (collection instanceof boolean[]) {\n        ((boolean[]) collection)[i] = (Boolean) value;\n      } else if (collection instanceof byte[]) {\n        ((byte[]) collection)[i] = (Byte) value;\n      } else if (collection instanceof double[]) {\n        ((double[]) collection)[i] = (Double) value;","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/reflection/wrapper/BaseWrapper.java#L64-L100","documentation":"The set-side counterpart of the null-collection read error: BaseWrapper.setCollectionValue throws a ReflectionException when an indexed assignment like 'items[0] = value' or 'map[key] = value' targets a property whose current value is null. MyBatis does not auto-create the missing collection; the container must already exist for element assignment to proceed.","triggerScenarios":"Result mapping that populates 'items[0]' when the 'items' List field of the target bean is null; MetaObject.setValue(\"map[key]\", v) when the map property was never constructed; a setter path writing into an uninitialized array property.","commonSituations":"DTOs with collection fields left null (no field initializer); partial result mappings that fill indexed entries before the container is created; deserialization of nested structures into beans without default constructors initializing collections.","solutions":["Initialize the collection/array field where it is declared: private List<Item> items = new ArrayList<>();","Pre-size arrays before indexed assignment: items = new Item[n];","In custom result handlers, create the container (setValue of the collection itself) before setting indexed elements","Verify resultMap nested collection mappings match the target bean's initialized structure"],"exampleFix":"// before\nprivate List<Item> items; // setValue(\"items[0]\", x) throws\n\n// after\nprivate List<Item> items = new ArrayList<>();","handlingStrategy":"validation","validationCode":"// ensure container exists before indexed writes\nif (metaObject.getValue(\"items\") == null) {\n  metaObject.setValue(\"items\", new java.util.ArrayList<>());\n}\nmetaObject.setValue(\"items[0]\", value);","typeGuard":null,"tryCatchPattern":"try { metaObject.setValue(\"items[0]\", v); }\ncatch (ReflectionException e) {\n  if (e.getMessage().contains(\"is null\")) { metaObject.setValue(\"items\", new ArrayList<>()); metaObject.setValue(\"items[0]\", v); }\n  else throw e;\n}","preventionTips":["Field-initialize collections: private List<X> items = new ArrayList<>();","For arrays, allocate the array before indexed writes","Verify resultMap nested mappings target initialized containers"],"tags":["reflection","null-safety","collections","result-mapping","mybatis"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}