How to call method of @UiField field in GWT

66 views Asked by At

here are my classes

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());
        }
    }

My problem is that when I call method setData(person), I get the following error

java.lang.NullPointerException: null in the line `firstnameView.setData(person.getFirstname())`

I think that @UiField FirstnameView firstnameView (and the lastnameView too) doesn't create an object. I really don't understand how gwt works in this case. Help me please resolve it and set data.

1

There are 1 answers

0
Thomas Broyer On BEST ANSWER

@UiFields are only "injected" when you call initAndBindUi on a UiBinder instance, passing an instance with the @UiFields as argument.

In other words, your PersonView constructor should begin with initializing a UiBinder instance and calling binder.initAndBindUi(this) before you can call setData().