0

I've code reacting to a click on canvas in Class Controller:

public void boardClick(MouseEvent mouseEvent) {
        if((int)mouseEvent.getSceneY() < Board.numOfCnR*Board.cellHeight && (int)mouseEvent.getSceneX() < Board.numOfCnR*Board.cellWidth){
            int row = (int)mouseEvent.getSceneY()/Board.cellHeight;
            int col = (int)mouseEvent.getSceneX()/Board.cellWidth;
            if(Game.gameBoard.getInitBoard()[row][col]==0){
                Game.gameBoard.setActive(new int[]{row, col});
                Game.game.drawGameBoard();
            }
        }
    }

And in class Game I have:

public class Game {
    public static Game game = new Game();
    public static Board gameBoard = Algorythm.generateBoard();
    private Stage stage;
    public Canvas graphicBoard;
    public void start(Stage stage){
        this.graphicBoard = (Canvas)stage.getScene().getRoot().getChildrenUnmodifiable().get(0);
        drawGameBoard();
    }
    public void drawGameBoard(){
        if(graphicBoard==null)
            this.graphicBoard = (Canvas)stage.getScene().getRoot().getChildrenUnmodifiable().get(0);
        gameBoard.drawBoard(graphicBoard.getGraphicsContext2D());
    }
}

Here's the stacktrace:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    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.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$ClickGenerator.postProcess(Scene.java:3564)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8200(Scene.java:3492)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3860)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
    ... 31 more
Caused by: java.lang.NullPointerException
    at sample.Game.drawGameBoard(Game.java:16)
    at sample.Controller.boardClick(Controller.java:99)
    ... 42 more

I cant understand why I'm getting NullPointerExcepton and how i should handle that? Most of the problem is that i dont really understand the idea of how controller should interact with other classes Here's the full code: https://github.com/kamilosm/SudokuSolver

  • Please copy the text of the complete stack trace into the question. Second, please stop shouting at us (that is, don't use all caps for the NullPointerException); it's impolite. – NomadMaker May 24 '20 at 09:49
  • You're right, I'm sorry for that – Kamil Maczuga May 24 '20 at 10:24
  • Game must extend Application, otherwise start isn't called. Best to work through a basic tutorial on javafx to understand its lifecycle. And don't use static scope ... – kleopatra May 24 '20 at 10:25

0 Answers0