{"record":{"id":"6eeb8abc04977de8","repo":"mybatis/mybatis-3","slug":"cannot-set-value-of-property-because-is","errorCode":null,"errorMessage":"Cannot set value of property '{}' because '{}' is null and cannot be instantiated on instance of {}. Cause:{}","messagePattern":"Cannot set value of property '(.+?)' because '(.+?)' is null and cannot be instantiated on instance of (.+?)\\. Cause:(.+?)","errorType":"exception","errorClass":"ReflectionException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/reflection/wrapper/BeanWrapper.java","lineNumber":180,"sourceCode":"      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {\n        return metaClass.hasGetter(name);\n      }\n      return metaValue.hasGetter(prop.getChildren());\n    }\n    return false;\n  }\n\n  @Override\n  public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {\n    MetaObject metaValue;\n    Class<?> type = getSetterType(prop.getName());\n    try {\n      Object newObject = objectFactory.create(type);\n      metaValue = MetaObject.forObject(newObject, metaObject.getObjectFactory(), metaObject.getObjectWrapperFactory(),\n          metaObject.getReflectorFactory());\n      set(prop, newObject);\n    } catch (Exception e) {\n      throw new ReflectionException(\"Cannot set value of property '\" + name + \"' because '\" + name\n          + \"' is null and cannot be instantiated on instance of \" + type.getName() + \". Cause:\" + e.toString(), e);\n    }\n    return metaValue;\n  }\n\n  private Object getBeanProperty(PropertyTokenizer prop, Object object) {\n    try {\n      Invoker method = metaClass.getGetInvoker(prop.getName());\n      try {\n        return method.invoke(object, NO_ARGUMENTS);\n      } catch (Throwable t) {\n        throw ExceptionUtil.unwrapThrowable(t);\n      }\n    } catch (RuntimeException e) {\n      throw e;\n    } catch (Throwable t) {\n      throw new ReflectionException(\n          \"Could not get property '\" + prop.getName() + \"' from \" + object.getClass() + \".  Cause: \" + t.toString(), t);","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/reflection/wrapper/BeanWrapper.java#L162-L198","documentation":"When MyBatis sets a nested property like 'customer.name' and 'customer' is null, BeanWrapper.instantiatePropertyValue tries to auto-instantiate the intermediate object via the ObjectFactory before setting into it. If that fails — no no-arg constructor, abstract class/interface, primitive, or the ObjectFactory throws — a ReflectionException wrapping the cause is thrown. The message names the property and the type it tried to create.","triggerScenarios":"MetaObject.setValue(\"customer.name\", v) where customer is null and Customer has no public no-arg constructor or is abstract/an interface; result mapping into a bean whose nested property type is an interface (e.g. List customization) or a class whose construction fails inside a custom ObjectFactory.","commonSituations":"DTOs with all-args constructors only (Lombok @AllArgsConstructor without @NoArgsConstructor); immutable nested types; nested property declared as an interface or abstract type; custom ObjectFactory with restrictive creation logic.","solutions":["Add a public no-arg constructor to the nested property type (with Lombok add @NoArgsConstructor)","Initialize the nested property at declaration: private Customer customer = new Customer();","Change the nested property's declared type from interface/abstract to a concrete instantiable class","If using a custom ObjectFactory, make sure it can create the type with no arguments"],"exampleFix":"// before\n@AllArgsConstructor // no default constructor\nclass Customer { ... }\n\n// after\n@AllArgsConstructor\n@NoArgsConstructor\nclass Customer { ... }","handlingStrategy":"validation","validationCode":"// verify the nested property type is auto-instantiable before setting deep paths\nClass<?> t = metaClass.getSetterType(\"customer\");\nboolean ok = !t.isInterface() && !Modifier.isAbstract(t.getModifiers())\n    && java.util.Arrays.stream(t.getConstructors()).anyMatch(c -> c.getParameterCount() == 0);\nif (!ok) { metaObject.setValue(\"customer\", new Customer()); }","typeGuard":"boolean instantiable(Class<?> c) {\n  return !c.isInterface() && !Modifier.isAbstract(c.getModifiers())\n      && java.util.Arrays.stream(c.getConstructors()).anyMatch(x -> x.getParameterCount() == 0);\n}","tryCatchPattern":"try {\n  metaObject.setValue(\"customer.name\", v);\n} catch (ReflectionException e) {\n  // message contains \"cannot be instantiated\": pre-create the intermediate object and retry once\n  if (e.getMessage().contains(\"cannot be instantiated\")) {\n    metaObject.setValue(\"customer\", new Customer());\n    metaObject.setValue(\"customer.name\", v);\n  } else throw e;\n}","preventionTips":["Add @NoArgsConstructor when using Lombok constructors on nested types","Initialize nested properties at field declaration","Keep nested property types concrete with public no-arg constructors"],"tags":["reflection","instantiation","nested-properties","lombok","mybatis"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}