1

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

我的实体类

@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 类

@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;
}

}

我的Vaadin面板

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

我哪里错了?

4

1 回答 1

2

看起来当您在binder.setItemDataSource(new BeanItem<MyEntity>(new MyEntity()));其 id 中创建实体时为空,因此尝试恢复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");
于 2013-11-07T10:29:34.273 回答