GWT中两个MultiUploader出错

时间:2015-01-28 13:26:50

标签: java gwt uibinder

我想创建2个MultiUploader元素,分别在jpg

中的pdf课程中上传CompositeGWT
public class MyClass extends Composite{
...
    @UiField(provided = true)
    MultiUploader muplDefPdf;
    @UiField(provided = true)
    MultiUploader muplDefJpg;
...
    private void setMuplDef(MultiUploader muplDef, String extention,
            IUploader.OnFinishUploaderHandler onFinishUploaderHandler,
            IUploader.OnCancelUploaderHandler onCancelUploaderHander) {
        muplDef = new MultiUploader();
        muplDef.setServletPath(muplDef.getServletPath());
        muplDef.setValidExtensions(extention);
        muplDef.setMaximumFiles(5);
        muplDef.avoidRepeatFiles(true);
        muplDef.addOnFinishUploadHandler(onFinishUploaderHandler);
        muplDef.addOnCancelUploadHandler(onCancelUploaderHander);
    }

    public MyClass() {
        setMuplDef(muplDefPdf, "pdf", onFinishUploaderHandlerPdf,
                onCancelUploaderHanderPdf);
        setMuplDef(muplDefJpg, "jpg", onFinishUploaderHandlerJpg,
                onCancelUploaderHanderJpg);
        initWidget(uiBinder.createAndBindUi(this));
    }

    private IUploader.OnFinishUploaderHandler onFinishUploaderHandlerJpg = new IUploader.OnFinishUploaderHandler() {
        @Override
        public void onFinish(IUploader uploader) {
           ...
        }
    };
    private IUploader.OnCancelUploaderHandler onCancelUploaderHanderJpg = new IUploader.OnCancelUploaderHandler() {
        @Override
        public void onCancel(IUploader uploader) {
          ...
        }
    };

    private IUploader.OnFinishUploaderHandler onFinishUploaderHandlerPdf = new IUploader.OnFinishUploaderHandler() {
        @Override
        public void onFinish(IUploader uploader) {
           ...
        }
    };
    private IUploader.OnCancelUploaderHandler onCancelUploaderHanderPdf = new IUploader.OnCancelUploaderHandler() {
        @Override
        public void onCancel(IUploader uploader) {
           ...
        }
    };
...
}

我的问题是我收到错误

 Exception caught: UiField muplDefJpg with 'provided = true' was null

为什么在muplDefJpg之前定义initWidget时出现此错误。 如何解决我的问题?

1 个答案:

答案 0 :(得分:2)

您遇到的问题与GWT 本身无关,但与Java本身无关。当你这样做时:

private void setMuplDef(MultiUploader muplDef, ...) {
    muplDef = new MultiUploader();

...你必须记住,在Java中,你总是在调用方法时复制参数的引用(更多关于here中的参数)。在这种情况下,您无法将muplDefPdf字段作为参数传递,期望它在方法中实例化。

为了使您的代码正常工作,您需要执行以下操作:

public MyClass() {
    muplDefPdf = new MultiUploader();
    setMuplDef(muplDefPdf, "pdf", onFinishUploaderHandlerPdf,
            onCancelUploaderHanderPdf);
    muplDefJpg = new MultiUploader();
    setMuplDef(muplDefJpg, "jpg", onFinishUploaderHandlerJpg,
            onCancelUploaderHanderJpg);
    initWidget(uiBinder.createAndBindUi(this));
}

...并删除muplDef = new MultiUploader();方法中的setMuplDef行。