1

I have a controller for an fxml file. The controller has a field public BorderPane mainBorderPane; which is supposed to be filled with the Border pane of the same fx:id found in the fxml file. When I try to access it from an inner class it gives a NullPointerExcetion

The clearBorderPaneCenter(); and the clearBorderPaneRight work just fine but when I run the cashSceneController.show() it crashes on the line mainBorderPane.setRight(rightLayout);

fxml file:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane fx:id="mainBorderPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Program.gui.MainSceneController">
   <top>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </top>
   <left>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </left>
   <center>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </center>
   <right>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </right>
   <bottom>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </bottom>
</BorderPane>

Simplified version of controller:

public class MainSceneController {

    MainSceneController.CashSceneController cashSceneController = new MainSceneController.CashSceneController();
    public BorderPane mainBorderPane;

    public void switchLayout(byte ID){
        //todo Make this work switchScene()

        clearBorderPaneCenter();
        clearBorderPaneRight();
        cashSceneController.show();
    }

    public void clearBorderPaneRight(){
        try {
            mainBorderPane.setRight(null);
            OutputHelper.log("cleared right of mainBorderPane");
        } catch (Exception e){
            OutputHelper.log("clearing right of mainBorderPane not required - already cleared");
        }
    }

    public void clearBorderPaneCenter(){
        try {
            mainBorderPane.setCenter(null);
            OutputHelper.log("cleared centre of mainBorderPane");
        } catch (Exception e){
            OutputHelper.log("clearing centre of mainBorderPane not required - already cleared");
        }
    }

    public class CashSceneController{
        VBox rightLayout;
        public void show() {
            setVBox();
            mainBorderPane.setRight(rightLayout);
        }

       public void setVBox(){
           rightLayout = new VBox();
           //......
       }
    }
}

This is how the fxml file is loaded

SceneController = new MainSceneController(new Scene(FXMLLoader.load(getClass().getResource("gui/MainScreen.fxml"))));

I hope that I explained my problem well enough. I'm new to stack overflow and have no idea of what is a good question

Edit: it appears that the problem is caused by the mainBorderPane not being assigned at all. The clearBorderPaneCenter and clearBorderPaneRight seem to not crash cause they are caught by the try catch. Any ideas why the mainBorderPane might not be getting assigned correctly?

wayne1512
  • 23
  • 1
  • 6
  • 1
    I guess `MainSceneController.switchLayout` is part of the stacktrace. Could you add information about how the instance of `MainSceneController` you call the method for is created and how the fxml corresponding to the controller is loaded? Also does the log contain exceptions? – fabian Sep 04 '18 at 14:58
  • The first thing that you might want to know is that MainSceneController extends SceneController – wayne1512 Sep 04 '18 at 15:01
  • how can I write code here without it coming to a mess? – wayne1512 Sep 04 '18 at 15:05
  • Click the [edit] link below the question to modify your post. – fabian Sep 04 '18 at 15:07
  • for now this will do as I dont know how to write code blocks here SceneController https://pastebin.com/VxbdCwp9 Constructors https://pastebin.com/tjaAYvMQ How it's created https://pastebin.com/Eczy6Zmj – wayne1512 Sep 04 '18 at 15:10
  • One thing that seems odd is that you create a controller using `FXMLLoader.load` and then wrap the scene you put the result into in another instance of `MainSceneController`. Probably reading this post https://stackoverflow.com/a/14190310/2991525 would get you some info on how to communicate with a controller. – fabian Sep 04 '18 at 15:46
  • BTW: you are calling `switchLayout` on the outer `MainSceneController` instance, i.e. the one assigned to `SceneController` in the last code snippet, I guess? – fabian Sep 04 '18 at 16:22
  • @fabian yes I am – wayne1512 Sep 04 '18 at 16:26

1 Answers1

0

You are missing the fxml annotation. It is required for the FXMLLoader to inject the BorderPane into the code

    @FXML      
    public BorderPane mainBorderPane;
Rengas
  • 593
  • 1
  • 6
  • 25