{"record":{"id":"da9cfabfa59f9c6e","repo":"mybatis/mybatis-3","slug":"cannot-get-the-value-because-the-property","errorCode":null,"errorMessage":"Cannot get the value '{}' because the property '{}' is null.","messagePattern":"Cannot get 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":47,"sourceCode":"public abstract class BaseWrapper implements ObjectWrapper {\n\n  protected static final Object[] NO_ARGUMENTS = {};\n  protected final MetaObject metaObject;\n\n  protected BaseWrapper(MetaObject metaObject) {\n    this.metaObject = metaObject;\n  }\n\n  protected Object resolveCollection(PropertyTokenizer prop, Object object) {\n    if (\"\".equals(prop.getName())) {\n      return object;\n    }\n    return metaObject.getValue(prop.getName());\n  }\n\n  protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {\n    if (collection == null) {\n      throw new ReflectionException(\"Cannot get the value '\" + prop.getIndexedName() + \"' because the property '\"\n          + prop.getName() + \"' is null.\");\n    }\n    if (collection instanceof Map) {\n      return ((Map) collection).get(prop.getIndex());\n    }\n    int i = Integer.parseInt(prop.getIndex());\n    if (collection instanceof List) {\n      return ((List) collection).get(i);\n    } else if (collection instanceof Object[]) {\n      return ((Object[]) collection)[i];\n    } else if (collection instanceof char[]) {\n      return ((char[]) collection)[i];\n    } else if (collection instanceof boolean[]) {\n      return ((boolean[]) collection)[i];\n    } else if (collection instanceof byte[]) {\n      return ((byte[]) collection)[i];\n    } else if (collection instanceof double[]) {\n      return ((double[]) collection)[i];","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/reflection/wrapper/BaseWrapper.java#L29-L65","documentation":"When evaluating an indexed property like 'list[0]' or 'map[key]', BaseWrapper.getCollectionValue first resolves the owning property via the MetaObject graph; if that resolved collection object is null, there is nothing to index into, so a ReflectionException is thrown naming the property. It means the container exists in the expression but its value is null at evaluation time.","triggerScenarios":"#{items[0].id} in a mapper when the 'items' field of the parameter object is null; MetaObject.getValue(\"map[key]\") when the map property itself is null; resultMap nested mappings indexing into a null List/Map/array property.","commonSituations":"Parameter DTO where a collection field was never initialized; conditional code that only populates a list in some branches; optional/absent keys in a Map parameter; a query returns null into a field that a subsequent expression indexes.","solutions":["Initialize the collection field in the parameter object (e.g. private List<Item> items = new ArrayList<>();) so it is never null","Guard the SQL fragment with <if test=\"items != null\"> before referencing items[0]","Ensure upstream code always sets the collection before the statement executes","For Map parameters, put an empty collection under the key instead of leaving it absent/null"],"exampleFix":"<!-- before -->\nSELECT * FROM t WHERE id = #{items[0].id}  <!-- items is null -->\n\n<!-- after -->\n<if test=\"items != null and items.size() > 0\">\n  SELECT * FROM t WHERE id = #{items[0].id}\n</if>","handlingStrategy":"validation","validationCode":"// before running the statement / MetaObject read\nObject coll = metaObject.getValue(\"items\");\nif (coll == null) {\n  // skip indexed access or initialize the collection first\n}","typeGuard":null,"tryCatchPattern":"try {\n  Object v = metaObject.getValue(\"items[0].id\");\n} catch (ReflectionException e) {\n  if (e.getMessage().contains(\"is null\")) { /* treat as absent value: default/skip */ }\n  else throw e;\n}","preventionTips":["Initialize collection fields at declaration in parameter/result beans","Wrap indexed expressions in <if test=\"... != null\"> in mappers","Service-layer null checks for optional collections before query invocation"],"tags":["reflection","null-safety","collections","mybatis"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}