public class Order {
@OneToOne
private Customer customer;
}
public class Customer {
@NotNull
@NotEmpty
private String firstName;
}
protected TextField firstName = new TextField();
BeanValidationBinder<Order> binder = new BeanValidationBinder<>(Order.class);
binder.bind(firstName, "customer.firstName");
binder.setBean(new Order());
causes
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.vaadin.data.BeanPropertySet.invokeWrapExceptions(BeanPropertySet.java:357)
at com.vaadin.data.BeanPropertySet.access$1(BeanPropertySet.java:354)
at com.vaadin.data.BeanPropertySet$NestedBeanPropertyDefinition.lambda$0(BeanPropertySet.java:219)
at com.vaadin.data.Binder$BindingImpl.convertDataToFieldType(Binder.java:899)
at com.vaadin.data.Binder$BindingImpl.initFieldValue(Binder.java:890)
at com.vaadin.data.Binder$BindingImpl.access$0(Binder.java:885)
at com.vaadin.data.Binder.lambda$1(Binder.java:1341)
at java.lang.Iterable.forEach(Iterable.java:75)
at com.vaadin.data.Binder.setBean(Binder.java:1341)
because the Customer bean is not created before trying to run its getter.
It was an explicit design decision not to create intermediate objects because in the general case, it is not possible (no guarantee of suitable constructors, might want to have the objects managed by ORM/DI/..., might need additional parameters to create them correctly). Perhaps it is not documented clearly enough, though.
Isn't it based on beans?
JavaBeans are classes that encapsulate many objects into a single object (the bean). They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods.
I think it would make sense to make it easy to use with beans and allow people with special needs to do their special things instead of forcing everybody to handle everything manually.
Automatically creating a new instance would be a major improvement in most common cases, would cause quite easily understandable errors messages in most other cases (more easily understandable than the current NPE from Method.invoke) and might cause some hard-to-debug situations in a very limited number of cases. Sounds like the net result from a DX point of view would be overwhelmingly positive.
What would be required in order to reverse the design decision?
I just ran into this weird looking NPE.
In my case I do not want to create the dependent object while the getter is being used: I just want the getter to return null in that case. I guess the framework can check on whether intermediate objects are null and just return null. When the setter is being used, only when the value to set is non-null, I would want the intermediate object to be created.
So:
Hello there!
It looks like this issue hasn't progressed lately. There are so many issues that we just can't deal them all within a reasonable timeframe.
There are a couple of things you could help to get things rolling on this issue (this is an automated message, so expect that some of these are already in use):
Thanks again for your contributions! Even though we haven't been able to get this issue fixed, we hope you to report your findings and enhancement ideas in the future too!
This needs to be re-verified with Vaadin 8.10 due https://github.com/vaadin/framework/pull/11758
Most helpful comment
Automatically creating a new instance would be a major improvement in most common cases, would cause quite easily understandable errors messages in most other cases (more easily understandable than the current NPE from
Method.invoke) and might cause some hard-to-debug situations in a very limited number of cases. Sounds like the net result from a DX point of view would be overwhelmingly positive.What would be required in order to reverse the design decision?