首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JavaFX错误-线程"JavaFX应用程序线程“java.lang.NullPointerException中出现异常

JavaFX错误-线程"JavaFX应用程序线程“java.lang.NullPointerException中出现异常
EN

Stack Overflow用户
提问于 2019-03-18 23:29:07
回答 1查看 973关注 0票数 -3

当我尝试在类Invoker的对象调用器中调用方法"undo“时,收到此错误消息

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());

奇怪的是,invoker类正常工作,并成功地在同一"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);
    }

}
EN

回答 1

Stack Overflow用户

发布于 2019-03-19 00:28:23

修复了!当然,我所要做的就是将我的invoker对象设置为static。愚蠢的小姐。

private static Invoker invoker;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55224797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档