Anchor Pane is null despite of being linked

209 views Asked by At

I´m developing a JavaFX app where I press a button and a new FXML will be loaded on my AnchorPane.

I´ve tried following both this tutorials ([Link1][1]) ([Link2][2]) but I couldn´t make it work.

By general I don´t have any problems with GenuineCoder´s tutorials but this time is quite different. I have understood that if you run initialize() method, all the components marked with @FXML annotation will be loaded. And that´s it. All the components are loaded and they all work except AnchorPane.

I´ve made sure several that fx:id is the same in Controller and in the .fxml file. So, why I´m getting a NullPointer?

Anyway, here it´s my code:

ControllerClass

public class AdministracionController implements Initializable{
    

@FXML
private ImageView butLogout;

@FXML
private JFXDrawer sidebarBox;

@FXML
private JFXHamburger hamburger;

@FXML
public AnchorPane rootPane;

private boolean isOpened;
private int transitionRate=1;
private HamburgerBasicCloseTransition transition;

    @FXML
    public void barSwitch() throws IOException{
        VBox box = FXMLLoader.load(getClass().getResource("/grd/pfc/menu/admon/admonToolbar.fxml"));
        sidebarBox.setSidePane(box); 
        sidebarBox.setMinWidth(0);
        if(transition==null){
           transition = new HamburgerBasicCloseTransition(hamburger);
        }
        transition.setRate(transitionRate);
        transition.play();
        if(isOpened){
            sidebarBox.setMinWidth(0);
            sidebarBox.close();
        }else{
            sidebarBox.setMinWidth(150);
            sidebarBox.open();
        }
        isOpened=!isOpened;
        transitionRate*=-1;
    }

    @FXML
    public void logout(){
        System.out.println("Sesión cerrada");
    }
    
    //This is the method where I try to load my FXML into my AnchorPane
    public void addEmpleado() throws IOException {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("/grd/pfc/menu/admon/addEmpleado.fxml"));
        rootPane.getChildren().setAll(pane);
    }

    
    public void addPerfil() {

    }

    
    public void editEmpleado() {

    }

    
    public void editPerfil() {

    }

    public void genRep() {

    }

    
    public void viewGraph() {

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
       
    }

}

MainFXML

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

<?import com.jfoenix.controls.JFXDrawer?>
<?import com.jfoenix.controls.JFXHamburger?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="grd.pfc.admon.AdministracionController">
   <children>
      <ImageView fx:id="butLogout" fitHeight="55.0" fitWidth="65.0" layoutX="828.0" layoutY="485.0" onMouseClicked="#logout" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../multimedia/Logout.png" />
         </image>
      </ImageView>
      <Text fill="#4059a9" layoutX="263.0" layoutY="57.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Administración">
         <font>
            <Font name="Arial Bold" size="48.0" />
         </font>
      </Text>
      <Separator layoutY="66.0" prefHeight="23.0" prefWidth="900.0" />
      <JFXDrawer fx:id="sidebarBox" defaultDrawerSize="150.0" layoutY="77.0" prefHeight="473.0" prefWidth="150.0" />
      <JFXHamburger fx:id="hamburger" layoutX="14.0" layoutY="40.0" onMouseClicked="#barSwitch" styleClass="jfx-hamburger" stylesheets="@admon.css" />
      <AnchorPane fx:id="rootPane" layoutX="150.0" layoutY="84.0" prefHeight="399.0" prefWidth="744.0" />
   </children>
</AnchorPane>

FXML to be loaded

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

<?import com.jfoenix.controls.JFXComboBox?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="406.0" prefWidth="751.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Text fill="#4059a9" layoutX="227.0" layoutY="47.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Añadir Empleado">
         <font>
            <Font name="Arial Bold" size="36.0" />
         </font>
      </Text>
      <VBox layoutX="125.0" layoutY="60.0" prefHeight="310.0" prefWidth="231.0">
         <children>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Nombre" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0" VBox.vgrow="ALWAYS">
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXTextField>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Fecha contratación" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0">
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXTextField>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Salario" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0" />
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Usuario" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0" />
         </children>
      </VBox>
      <VBox layoutX="395.0" layoutY="60.0" prefHeight="310.0" prefWidth="231.0">
         <children>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Apellidos" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0">
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXTextField>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Fecha Fin Contrato" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXTextField prefHeight="35.0" prefWidth="231.0">
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXTextField>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Tipo Contrato" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXComboBox prefHeight="25.0" prefWidth="234.0">
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXComboBox>
            <Text fill="#4059a9" strokeType="OUTSIDE" strokeWidth="0.0" text="Contraseña" textAlignment="CENTER" wrappingWidth="230.05078125">
               <font>
                  <Font name="Arial Bold" size="24.0" />
               </font>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Text>
            <JFXPasswordField>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </JFXPasswordField>
         </children>
      </VBox>
      <Separator layoutY="54.0" prefHeight="5.0" prefWidth="751.0" />
   </children>
</AnchorPane>

Sorry for the inconvenience, thanks a lot! [1]: https://www.youtube.com/watch?v=RJOza3XQk34 [2]: https://www.youtube.com/watch?v=ttD35jEo-f0

0

There are 0 answers