1

I am dealing with JavaFX , I am trying to get my graphical composant from my FXML page into the java classes, but it always return null value

Page.fxml

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>

<ScrollPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <content>
        <BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"  fx:id="Mycontent">
            <top>
                <fx:include source="TopMenu.fxml" />
            </top>
         <center>
                <fx:include source="Operation.fxml" />
         </center>
         <left>
             <fx:include source="SideBar_Inhumer.fxml" />
         </left>
         <bottom>
             <fx:include source="Footer.fxml" />
         </bottom>
        </BorderPane>
    </content>
</ScrollPane>

Controller.java

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.BorderPane;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    @FXML
    public BorderPane Mycontent;

    @FXML
    public void goToDemandeur(){
        System.out.println(Mycontent);

        //Mycontent.setCenter(FXMLLoader.load(getClass().getResource("Demandeur.fxml")));
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}

My code always print "null"

if I try for example Mycontent.getCenter()it gives me this error

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

(which normal because Mycontent is null, but why it is null? )

PS: my method goToDemandeur() is called into an other fxml page SideBar_Inhumer.fxml after a click onMouseClicked="#goToDemandeur"

SideBar_Inhumer.fxml

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<VBox layoutX="77.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
       <Pane >
           <children>
               <Label alignment="CENTER" text="Inhumer" >
                   <font>
                      <Font size="24.0" />
                   </font>
                   <padding>
                       <Insets bottom="20.0" left="25.0" right="25.0" top="30.0"  />
                   </padding>
               </Label>
           </children>
       </Pane>
       <Pane style="-fx-background-color: #F08080;" onMouseClicked="#goToDemandeur">
           <children>
               <Label text="Demandeur" >
                   <font>
                       <Font size="15.0" />
                   </font>
               <padding>
                  <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
               </padding>
               </Label>

           </children>
         <VBox.margin>
            <Insets />
         </VBox.margin>
       </Pane>
       <Pane style="-fx-background-color: #C6E2B9;">
           <children>
               <Label text="Defunt">
                   <font>
                       <Font size="15.0" />
                   </font>
                   <padding>
                       <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
                   </padding>
               </Label>
           </children>
       </Pane>
       <Pane style="-fx-background-color: #FBF6A5;">
           <children>
               <Label text="Emplacement">
                   <font>
                       <Font size="15.0" />
                   </font>
                   <padding>
                       <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
                   </padding>
               </Label>
           </children>
       </Pane>
       <Pane style="-fx-background-color: #FFCC99;">
           <children>
               <Label text="Prestataire">
                   <font>
                       <Font size="15.0" />
                   </font>
                   <padding>
                       <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
                   </padding>
               </Label>
           </children>
       </Pane>
       <Pane style="-fx-background-color: #D8B46D;">
           <children>
               <Label text="Opération">
                   <font>
                       <Font size="15.0" />
                   </font>
                   <padding>
                       <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
                   </padding>
               </Label>
           </children>
       </Pane>

   </children>
</VBox>

Main.java

    package View;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("../View/Page.fxml"));
        primaryStage.setTitle("Finalys");
        Screen screen = Screen.getPrimary();
        Rectangle2D bounds = screen.getVisualBounds();
        Scene scene =new Scene(root, 600, 475);
        primaryStage.setX(bounds.getMinX());
        primaryStage.setY(bounds.getMinY());
        primaryStage.setWidth(bounds.getWidth());
        primaryStage.setHeight(bounds.getHeight());
        primaryStage.setScene(scene);
        primaryStage.show();
        scene.getStylesheets().add(Main.class.getResource("bootstrap2.css").toExternalForm());


    }


    public static void main(String[] args) {
        launch(args);
    }
}
Bashir
  • 2,057
  • 5
  • 19
  • 44
  • 2
    Please can you show how you load Page.fxml? – Level_Up Jul 09 '19 at 07:55
  • 1
    Possible duplicate of [FXML Variables not binding](https://stackoverflow.com/questions/27810243/fxml-variables-not-binding) – Tschallacka Jul 09 '19 at 07:56
  • It is a good practice if you use camel case for your properties like `Mycontent` – Level_Up Jul 09 '19 at 08:02
  • 2
    I'm not that familiar with JavaFX but I'd assume it uses the Java Beans convention which is based on the basic Java code convention and that states that variables, fields etc. should use a name starting with a lower case character and then use camelcase. Especially the first requirement is meant to reduce confusion between class and field names, i.e. `Mycontent` looks like a class name but it isn't. Try `mycontent` or better `myContent` instead. – Thomas Jul 09 '19 at 08:05
  • @Tschallacka my method goToDemandeur is called into an other fxml page `SideBar_Inhumer.fxm` after a click `onMouseClicked="#goToDemandeur"` so moving the code into `initialize()` won't help me – Bashir Jul 09 '19 at 08:12
  • @Bashir You're not showing your loader code, so it's hard to see. Can you include the click handler code that initializes everything? Have you tried running this through a debugger with a breakpoint at the onclick? – Tschallacka Jul 09 '19 at 08:19
  • @Bashir Where's your `goToDemandeur()` code? – Tschallacka Jul 09 '19 at 08:29
  • @Tschallacka in Controller.java , it is mentionned in my question, I only print my BorderPane to see its value, when I try to edit the myContent I get Error `Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException` which is always mentionned in the question )) – Bashir Jul 09 '19 at 08:32
  • @Bashir you said you initialize the Page.fxml in the click handler. So show us that click handler. You're not showing where you initialize the Page.fxml – Tschallacka Jul 09 '19 at 08:44
  • @Tschallacka it is loaded in the Main.java , and in my Page.fxml , as you can see I have include tags to load other pages into my borderPane, and as you can see also , in SideBar_Inhumer.fxml you will find that I have `onMouseClicked="#goToDemandeur" ` I think it is so clear – Bashir Jul 09 '19 at 08:50
  • when I copied and pasted the code of my SideBar_Inhumer.fxml into Page.fxml it solved the problem, I mean by eliminating the include tag, but I want to seperate the code of each element and show them in the same page, my problem is that my controller is loaded in different pages which causes this problem (well I guess so, this is my first app with java fx) – Bashir Jul 09 '19 at 09:00
  • 4
    yeah, the problem is indeed the same controller for different fxmls: that's not useful, because there is a new controller instance created for each loading with only those fields injected that the fxml knows about, that's why SideBar cannot inject myContent which is simply kept null. Use different controllers for different fxmls and all will work fine :) – kleopatra Jul 09 '19 at 11:23
  • 3
    Possible duplicate of [One controller to 2 fxmls (JavaFX)](https://stackoverflow.com/questions/30464857/one-controller-to-2-fxmls-javafx) – Matt Jul 09 '19 at 13:20

0 Answers0