ListEditorWrapper NPE

时间:2012-12-14 14:56:19

标签: gwt

GWT 2.5.0

使用ListEditor的一个简单案例在下面失败了,我错过了什么?

public class OneBean {

    private String name;

    public OneBean() {
    }

    public OneBean(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "OneBean [name=" + name + "]";
    }

}
public class OneListEditor extends Composite implements
        IsEditor<ListEditor<OneBean, OneEditor>> {

    interface OneListUiBinder extends UiBinder<Widget, OneListEditor> {}
    OneListUiBinder uiBinder = GWT.create(OneListUiBinder.class);

    @UiField
    VerticalPanel panel;

    public OneListEditor() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Override
    public ListEditor<OneBean, OneEditor> asEditor() {
        return listEditor;
    }

    private ListEditor<OneBean, OneEditor> listEditor = ListEditor
            .of(new EditorSource<OneEditor>() {
                @Override
                public OneEditor create(int index) {
                    OneEditor widget = new OneEditor();
                    panel.insert(widget, index);
                    return widget;
                }
            });

}
public class OneEditor extends Composite implements Editor<OneBean> {

    interface OneUiBinder extends UiBinder<Widget, OneEditor> {}
    OneUiBinder uiBinder = GWT.create(OneUiBinder.class);

    @UiField
    TextBox name;

    public OneEditor() {
        initWidget(uiBinder.createAndBindUi(this));
    }

}
public class OneListEditorApp implements EntryPoint {

    @Override
    public void onModuleLoad() {
        List<OneBean> beans = new ArrayList<OneBean>();
        beans.add(new OneBean("1st bean"));
        beans.add(new OneBean("2nd bean"));

        OneListEditor oneListEditor = new OneListEditor();
        oneListEditor.asEditor().setValue(beans); // exception thrown here!

        RootPanel.get().add(oneListEditor);
    }

}
java.lang.NullPointerException: null
    at com.google.gwt.editor.client.adapters.ListEditorWrapper.attach(ListEditorWrapper.java:95)
    at com.google.gwt.editor.client.adapters.ListEditor.setValue(ListEditor.java:164)
    at OneListEditorApp.onModuleLoad ....
  void attach() {
    editors.addAll(editorSource.create(workingCopy.size(), 0));
    for (int i = 0, j = workingCopy.size(); i < j; i++) {
      chain.attach(workingCopy.get(i), editors.get(i)); // ListEditorWrapper NPE here!
    }
  }

@EDIT

根据@Thomas Broyer的回答,NPE在EditDriver连接到OneListEditor以下后消失了

interface OneEditorDriver extends
        SimpleBeanEditorDriver<OneBean, OneEditor> {}

OneEditorDriver driver = GWT.create(OneEditorDriver.class);

@Override
public ListEditor<OneBean, OneEditor> asEditor() {
    listEditor.setEditorChain(new EditorChain<OneBean, OneEditor>() {
        @Override
        public OneBean getValue(OneEditor subEditor) {
            return null;
        }
        @Override
        public void detach(OneEditor subEditor) {
        }
        @Override
        public void attach(OneBean object, OneEditor subEditor) {
            driver.initialize(subEditor);
            driver.edit(object);
        }
    });
    return listEditor;
}

1 个答案:

答案 0 :(得分:2)

您没有使用EditorDriver,因此ListEditor未使用EditorChain进行初始化,因此chainnull,因此为NPE。案例制作。

⇒使用EditorDriver(或不使用ListEditor