FieldGroup throws NPE on optional JPA association

This is a continuation of some prior posts where I am trying to implement a real-world application with a complex data model using JPAContainer, etc.

I am working on displaying various parts of the main Entity that also shows a few of it’s association data… some of this association data itself has associated data, etc.

Some of these associations are OPTIONAL (and are annotated that way in the @OneToOne) –

However, when I try to use that type of nested property in a Form, via a FieldGroup I get an NPE at :

Caused by: com.vaadin.data.util.MethodProperty$MethodException
at com.vaadin.data.util.NestedMethodProperty.getValue(NestedMethodProperty.java:205)
at com.vaadin.data.util.TransactionalPropertyWrapper.getValue(TransactionalPropertyWrapper.java:73)
at com.vaadin.ui.AbstractField.getDataSourceValue(AbstractField.java:301)
at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:626)
… 44 more
Caused by: java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.vaadin.data.util.NestedMethodProperty.getValue(NestedMethodProperty.java:201)

The usage is :
ourLayout.addComponent( fieldGroup.buildAndBind( “Offense Type”, “arrestInfo.offenseTypeItem.itemText” ), 2, 8, 3, 8 );

And yes, the bean data for arrestInfo.offenseTypeItem is null – which is acceptable, it’s allowed to be null since it’s optional …

I don’t think Vaadin should throw a NPE –

What I think would be very very slick is if Vaadin accepted the ?. Groovy type annotation (arrentInfo?.offenseTypeItem?.itemText) that indicates to just return a null if any of the path is null …

But even still, I would think the logic in the NestedMethodProperty.class could just protect this too and return null …

   @Override
    public T getValue() {
        try {
            Object object = instance;
            for (Method m : getMethods) {
                object = m.invoke(object);   <--- Line throwing the NPE
            }
            return (T) object;
        } catch (final Throwable e) {
            throw new MethodException(this, e);
        }
    }

Something simple like:

Object object = instance; for (Method m : getMethods) { object = m.invoke(object); if ( null == object ) return (T) object ; }

Anyway – meanwhile – any ideas on how to protect this from happening cleanly? I have about 15 fields on this one form alone that have optional associations …

Thanks!