Class that previously referred to a stackpane seems to "lose" this reference in the future in JAVA

Asked

Viewed 27 times

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

1 answer

0

Following the tip of Tiago Dinis checked the "error"! : debugging and accompanying the instantiation of the object:

    public ConstructChessBoard() {
        boardGUI = new GUIboard(boardModel, stackPane); //É aqui que a classe 
//Controller do FXML instância um objeto a partir do qual eu chamo o método 
//drawBoard() que vai desenhar no meu stackPane
    }

I checked that when the controller class of FXML is running its constructor method it still does not actually have instances of nodes/objects that have been declared in its body (i.e.: although I have declared in the Constructchessboard body.java: @FXML private StackPane stackPane; it only has an instance of that object AFTER executing its constructor class. Using the method:

    public void initialize() {
        boardGUI = new GUIboard(boardModel, stackPane);
    }

within the Constructchessboard class allows me to save my stackPane reference within the Guiboard class, because when you run initialize() it already has the objects "really mounted".

Thanks again Tiago Dinis!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.