Error: Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "application.Controller.moveUp()

Error: Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "application.Controller.moveUp()

我在我的 JavaFX 应用程序中遇到此错误。如果你知道如何解决它。请帮帮我:)

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "application.Controller.moveUp()" because "this.val$controller" is null
    at KeyEvent/application.Main.handle(Main.java:33)
    at KeyEvent/application.Main.handle(Main.java:1)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$KeyHandler.process(Scene.java:4105)
    at javafx.graphics/javafx.scene.Scene.processKeyEvent(Scene.java:2156)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2630)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:218)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:150)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent(GlassViewEventHandler.java:250)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:249)
    at javafx.graphics/com.sun.glass.ui.View.handleKeyEvent(View.java:548)
    at javafx.graphics/com.sun.glass.ui.View.notifyKey(View.java:972)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)

我的代码:

  package application;
        
    
    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.fxml.FXMLLoader;
    import javafx.stage.Stage;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.input.KeyEvent;
    
    
    
    public class Main extends Application {
        @Override
        public void start(Stage stage) {
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("Scene.fxml"));
                Parent root = loader.load();
                Controller controller = loader.getController();
                Scene scene = new Scene(root);
                
                scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
    
                    
                    
                    @Override
                    public void handle(KeyEvent event) {
                        //System.out.println(event.getCode());
                        //if I write -> if(controller != null) it doesn't work
                        switch(event.getCode()) {
                        case UP:
                            controller.moveUp();
                            break;
                        case DOWN:
                            controller.moveDown();
                            break;
                        case LEFT:
                            controller.moveLeft();
                            break;
                        case RIGHT:
                            controller.moveRight();
                            break;
                        default:
                            break;
                        
                        }
                    }
                    
                });
                
                stage.setScene(scene);
                stage.show();
                
            } catch(Exception e) {
                e.printStackTrace();
            }
            
        }
        
        public static void main(String[] args) {
            launch(args);
    }
}

控制器:

package application;


public class Controller {
    
    public void moveUp() {
        System.out.println("UP");
    }
    
    public void moveDown() {
        System.out.println("DOWN");
    }
    
    public void moveLeft() {
        System.out.println("LEFT");
    }
    
    public void moveRight() {
        System.out.println("RIGHT");
    }
    
}

例外状态:

because "this.val$controller" is null

要设置控制器,可以:

  1. 在调用 load

    之前在 fxml 加载器实例上显式调用 setController

  2. 在 FXML 中定义一个 fx:controller 属性。