#6 NullpointerException in BeanMorpher

closed
aalmiray
None
5
2007-04-14
2007-04-12
Ulrich Romahn
No

Trying to morph a bean A into another bean B where B has a property that does NOT exist in A causes a NPE.

The corresponding "offending" code are the following lines:
100 PropertyDescriptor sourcePd = PropertyUtils.getPropertyDescriptor( sourceBean, name );
101 if( sourcePd.getReadMethod() == null ){

As can be seen, when the property with the corresponding name does not exist, getPropertyDescriptor returns 'null' and the expression in the if will cause the NPE.

The corresponding fix should be:

if( sourcePd != null){
if( sourcePd.getReadMethod() == null ){
log.warn( "Property '" + sourceBean.getClass()
.getName() + "." + name + "' has no read method. SKIPPED." );
continue;
}
sourceType = sourcePd.getPropertyType();
}else{
log.warn( "Property '" + sourceBean.getClass()
.getName() + "." + name + "' does not exist. SKIPPED." );
continue;
}

Discussion

  • aalmiray
    aalmiray
    2007-04-14

    • assigned_to: nobody --> aalmiray
    • status: open --> closed
     
  • aalmiray
    aalmiray
    2007-04-14

    Logged In: YES
    user_id=1192026
    Originator: NO

    FIXED.
    Thanks for spotting it Ulrich.