Is there any way to prevent NPE when accessing a nested bean using commons-beanutils? Here is my code:
new BeanUtilsBean().getProperty(human, "parent.name");
In this case I want getProperty()
to either return empty string ("") when human.getParent() == null
or handle it in a way other that throwing an NPE.
They were thinking of adding language features to JDK7, but ultimately they weren't added
For now you'll have to manually check. You can just hack it and create a function like
public static void propertyHack(Object bean, String property, String nullreplace){
try{
return new BeanUtilsBean().getProperty(bean, property);
}
catch(NullPointerException npe){
return nullreplace;
}
}
Kind of sucks, but it will work.