使用ActionEvent打开新的fxml场景时发生NullPointerException

时间:2019-04-06 17:54:18

标签: java javafx nullpointerexception fxml

我在现场有不同的按钮。用户单击按钮时,应打开相同的fxml,但标题和标签不同,这取决于他单击的按钮。不幸的是,它在我尝试更改标签的行上也抛出了NullPointerException,也抛出了RuntimeException

代码:

    @FXML
    private Label training;

    protected void handleOpenOKSchwer(ActionEvent event) {      
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("Training.fxml"));

        Parent root1 = (Parent) loader.load();
        Stage stage = new Stage();
        stage.setTitle("Trainings-App");
        stage.getIcons().add(new Image("Icon.png"));
        stage.setScene(new Scene(root1));
        stage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
    training.setText("Training Upper");
    }    

例外:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
    at javafx.graphics/javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.controls/javafx.scene.control.Button.fire(Unknown Source)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Unknown Source)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Unknown Source)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
    at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.reflect.Trampoline.invoke(Unknown Source)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(Unknown Source)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(Unknown Source)
    ... 48 more
Caused by: java.lang.NullPointerException
    at Application.ActionHandler.handleOpenOKSchwer(ActionHandler.java:65)
    ... 58 more

0 个答案:

没有答案