无法沿BeanFieldGroup绑定嵌套属性

时间:2013-11-07 10:15:00

标签: java vaadin vaadin7

我正在开发一个vaadin应用程序,现在遇到以下问题。 我正在尝试沿着BeanFieldGroup绑定一个嵌套属性。

MyEntity Class

@Entity
public class MyEntity implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private MyEntityPK id;

//Others Property

public MyEntityPK getId() {
    return this.id;
}

public void setId(MyEntityPK id) {
    this.id = id;
}


}

MyEntityPK Class

@Embeddable
public class MyEntityPK implements Serializable {


@Column(name="CD_VARIABILE")
private String cdVariabile;

public String getCdVariabile() {
    return this.cdVariabile;
}
public void setCdVariabile(String cdVariabile) {
    this.cdVariabile = cdVariabile;
}

}

MyVaadinPanel

private BeanFieldGroup<MyEntity> binder;
binder = new BeanFieldGroup<MyEntity>(MyEntity.class);
binder.setItemDataSource(new BeanItem<MyEntity>(new MyEntity()));
variabileBinder.bind(new TextField(), "id.cdVariabile");

当我尝试将属性“id.cdVariabile”绑定到TextFiel时,我收到以下错误:

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:299)
at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:629)
... 48 more
   Caused by: java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.vaadin.data.util.NestedMethodProperty.getValue(NestedMethodProperty.java:201)
... 51 more

我哪里错了?

1 个答案:

答案 0 :(得分:2)

看起来当您在binder.setItemDataSource(new BeanItem<MyEntity>(new MyEntity()));中创建实体时,其id为null,因此尝试重置id.cdVariabile会导致NPE。 尝试添加PK:

    BeanFieldGroup<MyEntity> binder;
    binder = new BeanFieldGroup<MyEntity>(MyEntity.class);
    MyEntity myEntity = new MyEntity();
    myEntity.setId(new MyEntityPK());
    binder.setItemDataSource(new BeanItem<MyEntity>(myEntity));
    binder.bind(new TextField(), "id.cdVariabile");