简体   繁体   English

从Singleton触发GWT SimpleEventBus事件时出现Nullpointer [Umbrella]异常

[英]Nullpointer[Umbrella]Exception when firing GWT SimpleEventBus Event from Singleton

I'm being stuck for hours on this simple implementation of the GWT SimpleEventbus. 我在这个简单的GWT SimpleEventbus实现上被困了好几个小时。

Is there anything wrong with this setup? 这个设置有什么问题吗? What could be a possible reason for this? 这可能是什么原因?

I'm using the web.binderyEventbus 我正在使用web.binderyEventbus

import com.google.web.bindery.event.shared.SimpleEventBus;

The Eventbus is initalized in my Entrypoint Class. Eventbus在我的Entrypoint Class中被初始化。

public void onModuleLoad() {

    ApplicationEvents = new SimpleEventBus();

It's then injected into the Singleton LoadData and into the MainView (UIBinder class no MVP). 然后将它注入Singleton LoadData并注入MainView(UIBinder类没有MVP)。

    LoadData.initLoadData(ApplicationEvents);
    view = new MainView(ApplicationEvents);

initLoadData is basically an alias for the constructor, except that it's not returning anything. initLoadData基本上是构造函数的别名,除了它没有返回任何内容。

public class LoadData {

    private final SimpleEventBus eventBus;

    private LoadData(final SimpleEventBus bus) {
        eventBus = bus;
        ...
    }

    public static void initLoadData(final SimpleEventBus bus){ 
        if (instance == null){
            instance = new LoadData(bus);
        }
    }

The Eventbus is injected to the MainView like this. Eventbus像这样注入到MainView中。

public class MainView extends Composite {

    private final SimpleEventBus eventbus;

    public MainView(final SimpleEventBus bus) {
        eventbus = bus;
        ...

Then the handler is registered. 然后注册处理程序。

        eventbus.addHandler(EntriesReceiveEvent.TYPE, new EntriesReceiveEventHandler() {
            @Override
            public void onEntriesReceive(EntriesReceiveEvent event) {
            ...
            }
        });

And at the end of the method I have it call a method in LoadData. 在方法结束时,我让它在LoadData中调用一个方法。

        LoadData.getLoadData().fetchYears(); //alias for loadIndex

The loadIndex method in LoadData.java LoadData.java中的loadIndex方法

        private void loadIndex() {
                System.out.println(eventBus.toString());
                if(indexEntries == null){
                    AsyncCallback..
                    public void onSuccess(Map<Short, IndexEntry> result) {
                        eventBus.fireEvent(new EntriesReceiveEvent());
                        //^^ LoadData.java:190

EntriesReceiveEvent.java EntriesReceiveEvent.java

import com.google.gwt.event.shared.GwtEvent;

public class EntriesReceiveEvent extends GwtEvent<EntriesReceiveEventHandler> {

      public static Type<EntriesReceiveEventHandler> TYPE = 
              new Type<EntriesReceiveEventHandler>();

    @Override
    public Type<EntriesReceiveEventHandler> getAssociatedType() {
        return TYPE;
    }

    @Override
    protected void dispatch(EntriesReceiveEventHandler handler) {
        handler.onEntriesReceive(this);
    }
}

And it's handler EntriesReceiveEventHandler.java 它是处理程序EntriesReceiveEventHandler.java

import com.google.gwt.event.shared.EventHandler;

public interface EntriesReceiveEventHandler extends EventHandler {

    void onEntriesReceive(EntriesReceiveEvent event);
}

Finally the Exception. 最后是例外。

11:36:57.417 [ERROR] [] Uncaught exception escaped

com.google.web.bindery.event.shared.UmbrellaException: Exception caught: null
at com.google.web.bindery.event.shared.SimpleEventBus.doFire(SimpleEventBus.java:203)
at com.google.web.bindery.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:88)
at com.SoftwareEngineering.client.LoadData$2.onSuccess(LoadData.java:190)
at com.SoftwareEngineering.client.LoadData$2.onSuccess(LoadData.java:1)
at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:232)
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:258)
at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Unknown Source)

I thought that the Eventbus might not be the same as I didn't know if they're all talking to the same Eventbus. 我认为Eventbus可能不一样,我不知道他们是否都在谈论同一个Eventbus。 This site however proved my concerns to be wrong. 然而, 该网站证明了我的担忧是错误的。

Side question: Do you consider the Singleton in this context bad ? 附带问题:你认为在这种情况下单身人士好吗? It's a rather small project and the LoadData which handles all RPC requests needs to be globally available. 这是一个相当小的项目,处理所有RPC请求的LoadData需要全局可用。

Thank you for any help! 感谢您的任何帮助!

The NPE you have there is wrapped up in a Umbrella exception. 你在那里的NPE包含在一个伞例外。 This means that one of the event handlers actually threw the exception. 这意味着其中一个事件处理程序实际上抛出了异常。 The standard procedure in that case is to continue calling the rest of the handlers, then throw an umbrella exception containing all errors that happened while firing. 在这种情况下,标准过程是继续调用其余的处理程序,然后抛出一个伞状异常,其中包含触发时发生的所有错误。

To read the real exception, continue your way up the stack - read the rest of the log past where this happened, and see what happened in the handler (probably your own event handler code, or in some code that your handler calls). 要读取真正的异常,继续向上移动 - 读取发生这种情况的其余日志,并查看处理程序中发生的事情(可能是您自己的事件处理程序代码,或处理程序调用的某些代码)。

Another way to dig into issues like this: Use your IDE's debugger and set a breakpoint on all thrown NullPointerExceptions. 另一种深入研究这样的问题的方法:使用IDE的调试器并在所有抛出的NullPointerExceptions上设置断点。 This will pause the debugger the instant that a NPE happens so you can see it right away instead of digging through the logs. 这将在NPE发生的瞬间暂停调试器,这样您就可以立即看到它而不是挖掘日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM