JavaFX错误-线程“ JavaFX应用程序线程”中的异常java.lang.NullPointerException

时间:2019-03-18 15:29:07

标签: javafx javafx-8

当我尝试在类Invoker的对象调用程序中调用方法“撤消”时,出现此错误消息:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at view.MainWindow.lambda$2(MainWindow.java:112)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)

我猜测问题与javafx线程有关?我尝试使用Platform.runLater,但问题仍然存在。

我正在通过以下方式启动JavaFX窗口:

public static void main(String[] args) {
    Board board = new Board();
    Invoker invoker = new Invoker();
    TileBoardController tbc = new TileBoardController();
    MainWindow mainWindow = new MainWindow();
    com.sun.javafx.application.PlatformImpl.startup(()->{});
    mainWindow.launch(board, tbc.CreateTiles(board), invoker);
    com.sun.javafx.application.PlatformImpl.exit();
}

导致程序崩溃的事件位于该“ MainWindow”类的菜单栏中:

menuOptions.setOnAction(e -> invoker.undo());

奇怪的是,调用程序类起作用,并且成功地在同一“ MainWindow”对象的另一部分中使用:

tileBoardStatic[rank][file].setOnAction( e -> this.onTileClick(finalRank, finalFile));

private void onTileClick(int rank, int file) {

        invoker.storeAndExecute(new MovePiece(board, activeTile.getSquare(), tileBoardStatic[rank][file].getSquare()));

}

我已经用相同的方法尝试过invoker.undo(),并且有效。 有人知道这里出什么问题吗?

这是MainWindow中的所有代码:

public class MainWindow extends Application{

    public static final int TILE_SIZE = 100;
    public static final int WIDTH = 8;
    public static final int HEIGHT = 8;

    private Invoker invoker;
    private static Group tileGroup = null;
    private static Tile[][] tileBoardStatic = null;
    private Tile activeTile;
    private Board board;


    public void launch(Board board, Tile[][] tileBoard, Invoker invoker) {
        this.board = board;
        this.tileBoardStatic = tileBoard;
        this.invoker = invoker;
        tileGroup = new Group();
        for (int rank=0; rank<tileBoard.length; rank++) {
            for(int file=0; file<tileBoard[rank].length; file++) {
                final int finalRank = rank;
                final int finalFile = file;
                tileBoardStatic[rank][file].setOnAction( e -> this.onTileClick(finalRank, finalFile));
                tileGroup.getChildren().add(tileBoard[rank][file]);
            }
        }
        Application.launch(MainWindow.class);
    }


    //Handles click event for Tiles
    private void onTileClick(int rank, int file) {


        if (activeTile == null) {
            if (tileBoardStatic[rank][file].getSquare().getPiece() == null) 
                System.out.println("No piece in tile");
            else {
                if(tileBoardStatic[rank][file].getSquare().getPiece().isWhite() != board.getTurnIsWhite()) {
                    System.out.println("Not your turn");
                } else {
                    activeTile = tileBoardStatic[rank][file];
                    activeTile.getStyleClass().add("tile-clicked");
                }
            }
        } 
        else {
            activeTile.getStyleClass().removeAll("tile-clicked");
            //Creates and sends command to invoker
            invoker.storeAndExecute(new MovePiece(board, activeTile.getSquare(), tileBoardStatic[rank][file].getSquare()));

            //Repaints squares
            activeTile.paintSquare();
            tileBoardStatic[rank][file].paintSquare();

            activeTile = null;
        }



    }


    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane root = new BorderPane();
        primaryStage.getIcons().add(new Image(MainWindow.class.getResourceAsStream("resources/chess.png")));
        root.getChildren().addAll(tileGroup);
        root.setPrefSize(WIDTH * TILE_SIZE, HEIGHT * TILE_SIZE);

        Scene scene = new Scene(root);
        primaryStage.setTitle("MainWindow");
        primaryStage.setScene(scene);
        scene.getStylesheets().add(MainWindow.class.getResource("resources/stylesheet.css").toExternalForm());



        //Menu bar
        MenuBar menuBar = new MenuBar();
        Menu gameMenu = new Menu("Game");
        menuBar.getMenus().add(gameMenu);
        MenuItem menuExit = new MenuItem("Exit game");
        menuExit.setOnAction(e -> exit());
        gameMenu.getItems().add(menuExit);
        Menu menuOptions = new Menu("Edit");
        MenuItem menuUndo = new MenuItem("Undo");
        menuOptions.setOnAction(e -> invoker.undo());
        menuOptions.getItems().add(menuUndo);
        menuBar.getMenus().add(menuOptions);



        root.setTop(menuBar);
        primaryStage.show();

    }
    private void exit() {
        Platform.exit();
        System.exit(0);
    }

}

2 个答案:

答案 0 :(得分:1)

您错误地启动了应用程序。您的main方法初始化了一些对象,包括MainWindow的实例。然后,您在上述MainWindow实例上调用一个方法,该方法设置了一些字段,然后调用Application.launch。这就是问题所在的地方:对Application.launch的调用导致创建MainWindow新的,单独的实例。 JavaFX运行时使用的是这个实例。

由于start(Stage)方法是在您调用launch(Board,Tile[][],Invoker)的实例之外的实例上调用的,因此没有正确设置任何实例字段;最终导致您的NullPointerException。您将invoker设为静态的解决方案仅掩盖了更大问题的征兆-在典型生命周期之外初始化对象。

尝试将您的main方法更改为以下内容:

public static void main(String[] args) {
    Application.launch(MainWindow.class, args);
}

注意:如果main方法位于Application子句中,则可以改用launch(args)

然后将所有初始化逻辑移至以太Application#init()Application#start(Stage)(或至少从其中一个内部调用适当的方法)。需要 JavaFX Application Thread 的所有初始化都应在start方法中完成; init方法在 JavaFX-Launcher 线程上调用。

此外,使用Application.launch将隐式启动JavaFX运行时。您没有理由手动致电PlatformImpl.startup。除非没有其他选择,否则还应避免使用内部代码(例如com.sun.*)。如果您发现自己需要使用内部代码,请退后一步,检查您是否做错了什么。

答案 1 :(得分:0)

已修复!我要做的就是将我的调用者对象设置为静态对象。愚蠢的小姐。

private static Invoker invoker;