1

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!
    }
  }

@編集

さんの回答によると、下に配線し@Thomas BroyerたらNPEがなくなって、EditDriverOneListEditor

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

1 に答える 1

2

を使用していないEditorDriverため、はListEditorで初期化されていないEditorChainため、 NPE です。ケース製作。chainnull

⇒ を使うEditorDriver(または を使わないListEditor)

于 2012-12-14T15:14:38.763 に答える