有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有GoogleGuave EventBus和JavaFX FXMLUIObject的java NullPointerException

我希望有人能帮助我,因为我不知道: 问题是:

如果我用JavaFX和FXML在GUI中设置按钮的文本,效果会很好。 如果我从GoogleGuave事件总线运行相同的函数,它将返回一个NullPointer异常

下面的示例显示了我的问题: 函数setLabel()将调用两次。第一次从函数开始 初始化(URL arg0,ResourceBundle)。一切正常,一切正常,没有例外。第二次,如果事件到达,将从侦听调用它(LocalChangeEvent事件)。然后返回一个null指针异常

原因是什么

1 package de.iav.bottommenu;

  import java.io.PrintWriter;
  import java.io.StringWriter;
5 import java.net.URL;
  import java.util.ResourceBundle;

import javafx.fxml.FXML;
   import javafx.fxml.Initializable;
10 import javafx.scene.control.Button;
   import javax.inject.Inject;
import com.google.common.eventbus.Subscribe;
import de.iav.events.LocalChangeEvent;

15 public class BottomMenuPresenter implements Initializable {

   @FXML
   Button b_back, b_next, b_abort;

20 @Inject
public BottomMenuView mainBottomMenu;
Button test;
private ResourceBundle bundle;

25 // First function in the class. It will call setLabel() and 
// all is fine
public void initialize(URL arg0, ResourceBundle bundle) {

    this.bundle = bundle;
30  this.setLabel();
}

public void setLabel() {

35  this.bundle = ResourceBundle.getBundle("de.iav.bottommenu.bottommenu");
    try{

        b_next.setText(bundle.getString("b_next"));
        b_back.setText(bundle.getString("b_back"));
40      b_abort.setText(bundle.getString("b_abort"));

    // This exception fire only, if i call setLabel() from the 
    // Event-Bus function,listen(LocalChangeEvent event).
    }catch(Exception e){
45      System.out.println("Fehler: "  + e.getMessage()+e.toString());
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        System.out.println("FullStack: "  + sw.toString());
50      
    }       
}   
// This function is called if a LocalChangeEvent put  
// to the Event-Bus. The function will call setLabel() and 
55 // a Exception comes back :-(
@Subscribe
public void listen(LocalChangeEvent event) {
    System.out.println("Ich bin im Presenter");
    setLabel();
60 }

} 谢谢! 再见

编辑:

 java.lang.NullPointerException
    at de.iav.bottommenu.BottomMenuPresenter.setLabel(BottomMenuPresenter.java:38)
    at de.iav.bottommenu.BottomMenuPresenter.listen(BottomMenuPresenter.java:59)
    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.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)
    at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)
    at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)
    at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)
    at com.google.common.eventbus.EventBus.post(EventBus.java:275)
    at de.iav.language.LanguagePresenter.changeLanguage(LanguagePresenter.java:68)
    at de.iav.language.LanguagePresenter.b_changelanguageAction(LanguagePresenter.java:44)
    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 sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.scene.control.Button.fire(Unknown Source)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$242/1729325342.get(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

共 (0) 个答案