Search code examples
javajavafxnullpointerexceptionfxmlfxmlloader

NullPointerException and LoadException when using variables from one controller when initializing another. JavaFX


I am developing an app on netbeand and SceneBuilder with JavaFX and FXML files. I have different controllers for diferent content that popup in the main window. I call from the main controller for the FXML to popup, that has it's own sepparate controller. Here is the funcition in the main controller that calls for the new content:

public void mostrar_alumno(Alumno a) throws IOException {
    centro.getChildren().clear();
    FXMLLoader visualizador_alumno = new FXMLLoader(getClass().getResource("/visualizador_alumno/FXMLVisualizadorAlumno.fxml"));
    centro.getChildren().add(visualizador_alumno.load());
    FXMLVisualizadorAlumnoController controlador_visualizador_alumno = visualizador_alumno.getController();
    controlador_visualizador_alumno.setAlumno(a);
    boton_crear.setDisable(true);
}

Sorry for spanish names in the variables >:)

The thing is that with controlador_visualizador_alumno.setAlumno(a); I set that variable on the controller. This is the code from the other controller, the ones that has to popup:

public class FXMLVisualizadorAlumnoController implements Initializable {

@FXML
private ImageView foto;
@FXML
private TextField nombre;
@FXML
private TextField apellidos;
@FXML
private TextField correo;
private Tutorias misTutorias;
private Alumno alumno;

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    misTutorias = AccesoBD.getInstance().getTutorias();
    //Inicializamos los datos.
    nombre.setText(alumno.getNombre());
    apellidos.setText(alumno.getApellidos());
    correo.setText(alumno.getEmail());               
    
}    

public void setAlumno(Alumno a) {
    alumno = a;
}

}

So when the method from the main window is called and the new content is supposed to popup I get a LoadException in centro.getChildren().add(visualizador_alumno.load()); and a NullPointerException in nombre.setText(alumno.getNombre());

I guess this is because nombre.setText(alumno.getNombre()); is the initialize method and the variable alumno is seted from the first controller so I is not there yet, I am not sure. I will be very glad if you can help mi solve it. Any help is good. Thank you for your time.


Solution

  • As you say:

    This is because nombre.setText(alumno.getNombre()); is in the initialize() method and the variable alumno is set from the first controller so it is not there yet.

    To be precise, the initialize() method is invoked as part of the call to load(), which necessarily happens before you have access to the controller.

    All you need to do is move the code that depends on alumno to the setAlumno(...) method. This makes semantic sense too (when you change alumno, update the text fields that display its data):

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        misTutorias = AccesoBD.getInstance().getTutorias();
        //Inicializamos los datos.
        
    }    
    
    public void setAlumno(Alumno a) {
        alumno = a;
        nombre.setText(alumno.getNombre());
        apellidos.setText(alumno.getApellidos());
        correo.setText(alumno.getEmail());               
    }