-2

I have an fxml representation, wherein i have my fx:id defined to be ContentPane

<center>
            <Pane fx:id="ContentPane" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
         </center>

While in the main Application, i have

    public class Home_pageController extends Application {

    @FXML
    private Pane ContentPane;

    /**
     * Initializes the controller class.
     */
    /**
     * Initializes the controller class.
     */
    @Override
    public void start(Stage stage) throws Exception {
      Parent root = FXMLLoader.load(getClass().getResource("home_page.fxml"));           
        Scene scene = new Scene(root);
        stage.setTitle("Some scene");    
        stage.setScene(scene);         
        stage.show();
        MyGrid();
    }
    public static void main(String[] args) {
        launch(args);
    }
    public void MyGrid() throws IOException {        
        GridPane root = new GridPane();
        Label test = new Label("Test");
        root.add(test,0,0);
        ContentPane.getChildren().clear();
        ContentPane.getChildren().add(root);        
    }
   }

I do get the below error

 java.lang.NullPointerException
    at controllers.Home_pageController.MyGrid(Home_pageController.java:70)
    at controllers.Home_pageController.start(Home_pageController.java:62)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
fabian
  • 80,457
  • 12
  • 86
  • 114
Derin S
  • 55
  • 1
  • 10
  • 1
    Where are you calling `MyGrid()` from? Can you post the rest of the controller code, in particular the place where you declare and annotate `ContentPane`. Please update your code to use [proper naming conventions](https://en.wikipedia.org/wiki/Naming_convention_(programming)#Java): it is very difficult to read it if you don't follow the standard naming schemes. Also indicate which line is the one throwing the exception. – James_D Aug 10 '17 at 19:00
  • @James_D , do check the updated code pls. Thanks – Derin S Aug 10 '17 at 19:14
  • The `ContentPane` (sic) will only be initialized in the controller, it won't be initialized in the instance of the `Application` subclass that is created when you call `launch()`. – James_D Aug 10 '17 at 19:14

1 Answers1

2

If the fx:controller attribute is specified in the fxml, FXMLLoader uses the constructor of the class specified as attribute value to construct a new instance of the controller.

An existing instance of the controller is not automatically reused.

This is why the ContentPane field of the application instance that is started is never modified and remains null.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • thanks for that, what do you suggest i do? – Derin S Aug 10 '17 at 19:16
  • @DerinS communicating with the controller to pass the necessary info to it, see https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml BTW: Using the application class as controller should IMHO be avoided. – fabian Aug 10 '17 at 19:20