这是我的课程
public class PersonView extends Composite{
private Person person;
@UiField
FirstnameView firstnameView;
@UiField
LastnameView lastnameView;
public PersonView(Person person) {
setData(person);
}
public void setData(Person person) {
firstnameView.setData(person.getFirstname());
lastnameView.setData(person.getLastname());
}
}
public class FirstnameView extends Composite{
private Firstname firstname;
@UiField
TextBox firstnameTextBox;
public FirstnameView() {
}
public void setData(Firstname firstname) {
firstnameTextBox.setValue(firstname.getFirstname());
}
}
public class LastnameView extends Composite{
private Lastname lastname;
@UiField
TextBox lastnameTextBox;
public LastnameView() {
}
public void setData(Lastname lastname) {
lastnameTextBox.setValue(lastname.getLastname());
}
}
我的问题是,当我调用方法setData(person)
时,我收到以下错误
java.lang.NullPointerException: null in the line `firstnameView.setData(person.getFirstname())`
我认为@UiField FirstnameView firstnameView
(以及lastnameView
)也不会创建对象。我真的不明白gwt在这种情况下是如何工作的。请帮我解决并设置数据。
答案 0 :(得分:1)
@UiField
实例上调用initAndBindUi
并传递一个以UiBinder
为参数的实例时, @UiField
仅被“注入”。
换句话说,您的PersonView
构造函数应该首先初始化UiBinder
个实例并调用binder.initAndBindUi(this)
,然后才能调用setData()
。