{"record":{"id":"9bee32b9bba4e523","repo":"mybatis/mybatis-3","slug":"could-not-get-property-from-cause","errorCode":null,"errorMessage":"Could not get property '{}' from {}.  Cause: {}","messagePattern":"Could not get property '(.+?)' from (.+?)\\.  Cause: (.+?)","errorType":"exception","errorClass":"ReflectionException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/reflection/wrapper/BeanWrapper.java","lineNumber":197,"sourceCode":"    } 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);\n    }\n  }\n\n  private void setBeanProperty(PropertyTokenizer prop, Object object, Object value) {\n    try {\n      Invoker method = metaClass.getSetInvoker(prop.getName());\n      Object[] params = { value };\n      try {\n        method.invoke(object, params);\n      } catch (Throwable t) {\n        throw ExceptionUtil.unwrapThrowable(t);\n      }\n    } catch (Throwable t) {\n      throw new ReflectionException(\"Could not set property '\" + prop.getName() + \"' of '\" + object.getClass()\n          + \"' with value '\" + value + \"' Cause: \" + t.toString(), t);\n    }\n  }","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/reflection/wrapper/BeanWrapper.java#L179-L215","documentation":"BeanWrapper.getBeanProperty invokes the property's read method via reflection. If the getter itself throws (any non-runtime Throwable, unwrapped via ExceptionUtil.unwrapThrowable), MyBatis wraps it in a ReflectionException naming the property, the owner class, and the cause. The real failure is inside the getter's code — the exception message's Cause is the key.","triggerScenarios":"A getter that dereferences an uninitialized internal field and throws NPE; a getter performing computation/parse (e.g. getFullName() concatenating nulls, getDuration() dividing by zero); a getter throwing a checked exception; mapping a transient/computed property in a resultMap or #{expr} evaluation.","commonSituations":"Lazy-loading proxies inside getters failing after session close; getters with business logic that break on edge-case state (null fields, empty strings); computed getters referenced in mapper expressions that assume populated state.","solutions":["Read the Cause in the exception message and fix the underlying code in the getter (usually an NPE on an unset field)","Make the getter defensive: return null/default when its dependencies are not initialized","Avoid referencing computed/logic-heavy getters in mapper expressions; map stored fields instead","Initialize the fields the getter depends on (constructor or field initializers)"],"exampleFix":"// before\npublic String getFullName() { return firstName.concat(\" \").concat(lastName); } // NPE when null\n\n// after\npublic String getFullName() {\n  return Stream.of(firstName, lastName).filter(Objects::nonNull).collect(Collectors.joining(\" \"));\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  Object v = metaObject.getValue(\"fullName\");\n} catch (ReflectionException e) {\n  Throwable cause = e.getCause(); // inspect: the getter itself failed\n  // fix getter defensively; do not blanket-catch and continue in production paths\n  throw new IllegalStateException(\"Getter failed for fullName: \" + cause, cause);\n}","preventionTips":["Keep getters side-effect free and null-tolerant","Do not reference computed getters in mapper expressions unless they handle unset state","Initialize fields that getters depend on"],"tags":["reflection","getter","runtime-exception","mybatis"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}