0
Hail to you, programmers! Ask for help with a Java Javafx code: - I have the controller class where all graphical elements with multiple @FXML are declared, including a stackPane variable. I pass this stackPane as a reference for the Guiboard class when I instill it, so that within this last one I will have a "link" pro stackPane. - Still inside Guiboard, at a certain time I want to load a chess position on the board (specified in the Fenactual string), I call the "drawBoard" method below (which is inside Guiboard.java):
public void drawBoard(String FENatual, StackPane stackPane2) {
drawEmptyBoard(stackPane2);
refreshBoardImage(stackPane2, boardModel.getBoardASCII());
}
The thing is: if I don’t pass the reference of the stackPane in the drawBoard method from my controller class, it doesn’t find the stackPane of the Guiboard class that I related when I instated the "boardGUI" object also within the controller class:
//Classe controladora:
public class ConstructChessBoard {
private StackPane stackPane;
public ConstructChessBoard() {
boardGUI = new GUIboard(boardModel, stackPane);
}
public class GUIboard {
private StackPane stackPane;
public GUIboard(BoardModel boardModel, StackPane stackPane) { //Método construtor da GUIBoard
this.stackPane = stackPane;
}
The error message is:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "javafx.scene.layout.StackPane.getChildren()" because "stackPane" is null
at groupId/groupId.artifactId.view.GUIboard.drawEmptyBoard(GUIboard.java:278)
at groupId/groupId.artifactId.view.GUIboard.drawBoard(GUIboard.java:552)
at groupId/groupId.artifactId.controller.ConstructChessBoard.lambda$0(ConstructChessBoard.java:278)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
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.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:4098)
at javafx.graphics/javafx.scene.Scene.processKeyEvent(Scene.java:2157)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2625)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$1(GlassViewEventHandler.java:248)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
at javafx.graphics/com.sun.glass.ui.View.handleKeyEvent(View.java:547)
at javafx.graphics/com.sun.glass.ui.View.notifyKey(View.java:971)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
at java.base/java.lang.Thread.run(Thread.java:832)
The error of:
public void drawEmptyBoard(StackPane stackPane) {
gridPane = new GridPane();
stackPane.getChildren().add(gridPane); //O Erro .....drawEmptyBoard(GUIboard.java:278) é gerado nessa linha
}
Try to start in debug mode and see if any of these variables or any of the parameters is
null
– Tiago Dinis