I have a JavaBean called UserDTO:
UserDTO
int id;
int roleId;
int addressId;
String name;
I have another JavaBean called UserView
UserView
String id;
String roleId;
String addressId;
String name;
I get a description of UserView using BeanUtils.describe() which I then use
to populate UserDTO using BeanUtils.populate()....
UserView view = new UserView();
view.setId("1");
Map description = BeanUtils.describe(view);
UserDTO user = new UserDTO();
BeanUtils.populate(user, view);
I get the following stack trace when it attempts to set the "roleId" field
with a null value.
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at
org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.j
ava:1612)
at
org.apache.commons.beanutils.PropertyUtils.setNestedProperty(PropertyUtils.j
ava:1506)
at
org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.java:15
36)
at
org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:681)
at com.newatlanta.poc.UserViewForm.main(UserViewForm.java:275)
The reason for this is due to the fact that the value is not converted to
the appropriate type
and the null value is passed through to the PropertyUtils where the
exception is thrown.
My expectation would be that any primitives that were assigned a value of
null, would be assigned the default value for the respective primitive type.
(which in my case should be zero ).
I fixed this problem by adding a condition to the scalar conversion block.
// value into scalar
if (value instanceof String
|| (value == null && type.isPrimitive())) { <==== new condition here
....
}
This fixed my problem, but I'm unsure why this wasn't added in the first
place. Am I missing something here?
robert
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>