Back Forum Reply New

NullValueInNestedPathException solution

Hi,

I'm not sure if it is the best solution, but to solve this issue, I created a custom BeanWrapperImpl and used it in my application. All 'the small ' classes involved in this solution are:
- XServletRequestDataBinder (custom class used by controller)- XBeanPropertyBindingResult (custom class used by XServletRequestDataBinder)- XBeanWrapperImpl (custom BeanWrapperImpl that catchs the NullValueInNestedPathException)

Here is a sample:Code:
public class MyForm extends SimpleFormController {

@Override
protected ServletRequestDataBinder createBinder(fromServletRequest request, Object command)
throws Exception {
ServletRequestDataBinder binder = new XServletRequestDataBinder(command, getCommandName());
prepareBinder(binder);
initBinder(request, binder);
return binder;
}

}
The code of XServletRequestDataBinder is:Code:
public class XServletRequestDataBinder extends ServletRequestDataBinder {

private XBeanPropertyBindingResult bindingResult;

public XServletRequestDataBinder(Object target) {
super(target);
}

public XServletRequestDataBinder(Object target, String objectName) {
super(target, objectName);
}

@Override
protected AbstractPropertyBindingResult getInternalBindingResult() {
if (this.bindingResult == null) {
this.bindingResult = new XBeanPropertyBindingResult(getTarget(), getObjectName());
}
return this.bindingResult;
}

}
The code of XBeanPropertyBindingResult is:Code:
public class XBeanPropertyBindingResult extends BeanPropertyBindingResult {

public XBeanPropertyBindingResult(Object target, String objectName) {
super(target, objectName);
}

@Override
protected BeanWrapper createBeanWrapper() {
return new XBeanWrapperImpl(getTarget());
}
}
The code of XBeanWrapperImpl is:Code:
public class XBeanWrapperImpl extends BeanWrapperImpl {

public XBeanWrapperImpl(Object target) {
super(target);
}

@Override
public void setPropertyValue(String property, Object value) throws BeansException {
try {
super.setPropertyValue(property, value);
} catch (NullValueInNestedPathException e) {
// do nothing
}
}

@Override
public Object getPropertyValue(String propertyName) throws BeansException {
try {
return super.getPropertyValue(propertyName);
} catch (NullValueInNestedPathException e) {
return null;
}
}
}
I don't like the idea of having a factory to create new entities, because in many use cases, the user will switch an entity already created in the system and stored in database. In some cases I use a propertyEditor to populate the entity in the right way.

I'm happy with this solution...

Any thoughts ?
¥
Back Forum Reply New