我正在开发 vaadin 应用程序,现在遇到以下问题。 我正在尝试沿 BeanFieldGroup 绑定(bind)嵌套属性。
MyEntity 类
@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;
}
}
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”绑定(bind)到 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
我哪里错了?
最佳答案
看起来就像您在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");
关于java - 无法沿 BeanFieldGroup 绑定(bind)嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19833228/