I am making an interface (Student) in which when opened it should be seen as below.
Followed by this, if you want to modify a student, you would open this:
-The problem is that when I run the program and click on the button to open the student panel, I get this series of errors:
I have carefully reviewed my code and maybe the error is in the controllers when trying to handle the arrayList with observableList of Java fx; "AdministracionEscolarController" simply contains the methods to open the windows from the main interface and I don't think the problem is there and neither is mainApp because the load of the views fxml is there, if someone could lend me a hand I would greatly appreciate it!
A part of my constructor class is:
public class Colegio implements Serializable {
// Declaracion de Atributos privados (ArrayList respectivo de cada clase)
private static final long serialVersionUID = 1L;
private ArrayList<Profesor> listaProfesores;
private ArrayList<Materia> listaMaterias;
private ArrayList<Estudiante> listaEstudiantes;
private ArrayList<RegistroMaterias> listaRegistroMaterias;
/*
* Constructor de lo que para nosotros es la clase principal, Desde esta clase
* manejaremos la creacion respectiva de cada Profesor, materia,
* estudiante y registros de materias que se realizen y seran alojados en un
* arrayList respectivamente
*/
Constructor clase Colegio
public Colegio() {
listaProfesores= new ArrayList<>();
listaMaterias= new ArrayList<>();
listaEstudiantes= new ArrayList<>();
listaRegistroMaterias= new ArrayList<>();
}
The main student window controller is:
public class CRUDEstudianteController implements Initializable {
@FXML
private Button btnAgregar;
@FXML
private TableView<Estudiante> tblestudiantes;
@FXML
private TableColumn colCED;
@FXML
private TableColumn colNombre;
@FXML
private TableColumn colGrado;
@FXML
private ObservableList<Estudiante> estudiantes;
@FXML
private ObservableList<Estudiante> filtroEst;
@FXML
private Button btnModificar;
@FXML
private Button btnEliminar;
@FXML
private TextField txtFiltrarNombre;
@FXML private static MainApp cole;
public void initialize(URL location, ResourceBundle resources) {
cole = AdministracionEscolarController.getCole();
estudiantes = FXCollections.observableArrayList();
filtroEst = FXCollections.observableArrayList();
this.tblestudiantes.setItems(estudiantes);
this.colNombre.setCellValueFactory(new PropertyValueFactory("Nombre"));
this.colCED.setCellValueFactory(new PropertyValueFactory("# Documento"));
this.colGrado.setCellValueFactory(new PropertyValueFactory("Grado"));
}
@FXML
private void agregarPersona(ActionEvent event) {
try {
// Cargo la vista
FXMLLoader loader = new FXMLLoader(getClass().getResource("EstudianteDialogVista.fxml"));
// Cargo la ventana
Parent root = loader.load();
// Cojo el controlador
CRUDEstudianteDialog controlador = loader.getController();
controlador.initAtributtes(estudiantes);
// Creo el Scene
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(scene);
stage.showAndWait();
// cojo la persona devuelta
Estudiante p = controlador.getEstudiante();
if (p != null) {
estudiantes.add(p);
if (p.getNombreEstudiante().toLowerCase().contains(this.txtFiltrarNombre.getText().toLowerCase())) {
this.filtroEst.add(p);
}
this.tblestudiantes.refresh();
}
} catch (IOException e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setTitle("Error");
alert.setContentText(e.getMessage());
alert.showAndWait();
}
}
@FXML
private void modificar(ActionEvent event) {
Estudiante p = this.tblestudiantes.getSelectionModel().getSelectedItem();
if (p == null) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setTitle("Error");
alert.setContentText("Debes seleccionar un estudiante!");
alert.showAndWait();
} else {
try {
// Cargo la vista
FXMLLoader loader = new FXMLLoader(getClass().getResource("EstudianteDialogVista.fxml"));
// Cargo la ventana
Parent root = loader.load();
// Cojo el controlador
CRUDEstudianteDialog controlador = loader.getController();
controlador.initAtributtes(estudiantes,p);
// Creo el Scene
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(scene);
stage.showAndWait();
// cojo la persona devuelta
Estudiante pSeleccionado = controlador.getEstudiante();
if (pSeleccionado != null) {
if (!pSeleccionado.getNombreEstudiante().toLowerCase().contains(this.txtFiltrarNombre.getText().toLowerCase())) {
this.filtroEst.remove(pSeleccionado);
}
this.tblestudiantes.refresh();
}
} catch (IOException e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setTitle("Error");
alert.setContentText(e.getMessage());
alert.showAndWait();
}
}
}
@FXML
private void eliminar(ActionEvent event) {
Estudiante p = this.tblestudiantes.getSelectionModel().getSelectedItem();
if (p == null) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setTitle("Error");
alert.setContentText("Debes seleccionar un estudiante");
alert.showAndWait();
} else {
// Elimino la persona
this.estudiantes.remove(p);
this.filtroEst.remove(p);
this.tblestudiantes.refresh();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText(null);
alert.setTitle("Info");
alert.setContentText("Estudiante eliminado");
alert.showAndWait();
}
}
@FXML
private void filtrarNombre(KeyEvent event) {
String filtroNombre = this.txtFiltrarNombre.getText();
// Si el texto del nombre esta vacio, seteamos la tabla de personas con el original
if (filtroNombre.isEmpty()) {
this.tblestudiantes.setItems(estudiantes);
} else {
// Limpio la lista
this.filtroEst.clear();
for (Estudiante p : this.estudiantes) {
if (p.getNombreEstudiante().toLowerCase().contains(filtroNombre.toLowerCase())) {
this.filtroEst.add(p);
}
}
this.tblestudiantes.setItems(filtroEst);
}
}
}
The window controller code in case you want to modify a student is:
public class CRUDEstudianteDialog implements Initializable {
@FXML
private TextField tfce;
@FXML
private TextField tfne;
@FXML
private TextField txtEdad;
@FXML
private TextField txtApellidos;
@FXML
private TextField txtNombre;
@FXML
private Button btnGuardar;
@FXML
private Button btnSalir;
@FXML
private DatePicker dp;
@FXML
private ComboBox comboBox;
@FXML
private ComboBox comboBoxGradoEst;
@FXML private Label comboBoxgradosMateriasLabel;
@FXML
private Label DatePickerLabel;
private ObservableList<Estudiante> estudiantes;
private Estudiante estudiante;
@FXML private static MainApp cole;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
cole = AdministracionEscolarController.getCole();
estudiantes = FXCollections.observableArrayList();
//estudiantes = FXCollections.observableArrayList();
comboBoxGradoEst.getItems().addAll( // Aqui añadimos los años
// dados de la materia en el
// comboBox
"6", "7", "8", "9", "10", "11");
}
public void initAtributtes(ObservableList<Estudiante> estudiantes) {
this.estudiantes = estudiantes;
}
public void initAtributtes(ObservableList<Estudiante> personas, Estudiante estudiante) {
this.estudiantes = personas;
this.estudiante = estudiante;
// cargo los datos de la persona
this.tfne.setText(this.estudiante.getNombreEstudiante());
this.tfce.setText(this.estudiante.getIDEstudiante());
this.comboBoxGradoEst.setAccessibleHelp((this.estudiante.getGrado()));
this.dp.setPromptText(this.estudiante.getFechaNacimiento()+ "");
}
@FXML
private void salir(ActionEvent event) {
this.estudiante = null;
// Cerrar la ventana
Stage stage = (Stage) this.btnGuardar.getScene().getWindow();
stage.close();
}
@FXML
private void agregarEstudiante(ActionEvent event) {
String cedEstudiante, nomEstudiante, fNacimiento;
String grado;
boolean agregado;
cedEstudiante = tfce.getText(); /*
* Guardado de la cedula y nombre del
* estudiante de los textFields
*/
nomEstudiante = tfne.getText();
fNacimiento = (String) (dp.getValue().toString()); // Guardado de la
// fecha de
// nacimiento segun
// la fecha del
// datePicker
grado = (String)(comboBoxGradoEst.getSelectionModel().getSelectedItem());
Estudiante est = cole.cole.agregarEstudiante(cedEstudiante, nomEstudiante, fNacimiento, grado); // Guardado
// de
// el
// estudiante
// en
// el
// mismo
// objeto
// del
// proyecto
// con
// sus
// respectivos
// atributos
// del
// constructor
// alojados
// en
// el colegio
// Compruebo si la persona existe
if (!estudiantes.contains(est)) {
// Modificar
if (this.estudiantes != null) {
// Modifico el objeto
this.estudiante.setIDEstudiante(cedEstudiante);
this.estudiante.setNombreEstudiante(nomEstudiante);
this.estudiante.setFechaNacimiento(fNacimiento);
this.estudiante.setGrado(grado);
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText(null);
alert.setTitle("Informacion");
alert.setContentText("Se ha modificado correctamente");
alert.showAndWait();
} else {
// insertando
this.estudiante = est;
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText(null);
alert.setTitle("Informacion");
alert.setContentText("Se ha añadido correctamente");
alert.showAndWait();
}
// Cerrar la ventana
Stage stage = (Stage) this.btnGuardar.getScene().getWindow();
stage.close();
} else {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setTitle("Error");
alert.setContentText("La persona ya existe");
alert.showAndWait();
}
}
public Estudiante getEstudiante() {
return estudiante;
}
public void comboBoxSeleccionadoGradoPerteneceM() {
this.comboBoxgradosMateriasLabel
.setText("Grado Seleccionado: \n" + comboBoxGradoEst.getValue().toString());
}
public void datePickerSeleccionado() {
this.DatePickerLabel.setText("Fecha: \n" + dp.getValue().toString());
}
The code of StudentWindow.fxml :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="433.0" prefWidth="639.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="admin.colegial.view.CRUDEstudianteController">
<children>
<Button fx:id="btnAgregar" layoutX="60.0" layoutY="394.0" mnemonicParsing="false" onAction="#agregarPersona" prefHeight="25.0" prefWidth="149.0" text="Agregar Estudiante" />
<TableView fx:id="tblPersonas" layoutX="34.0" layoutY="67.0" prefHeight="306.0" prefWidth="589.0">
<columns>
<TableColumn fx:id="colNombre" prefWidth="215.0" text="Nombre" />
<TableColumn fx:id="colApellidos" prefWidth="237.0" text="# Documento" />
<TableColumn fx:id="colEdad" prefWidth="123.0" text="Grado" />
</columns>
</TableView>
<Button fx:id="btnModificar" layoutX="241.0" layoutY="394.0" mnemonicParsing="false" onAction="#modificar" prefHeight="25.0" prefWidth="169.0" text="Modificar Estudiante" />
<Button fx:id="btnEliminar" layoutX="444.0" layoutY="394.0" mnemonicParsing="false" onAction="#eliminar" prefHeight="25.0" prefWidth="156.0" text="Eliminar Estudiante" />
<Label layoutX="40.0" layoutY="29.0" text="Filtrar por nombre:" />
<TextField fx:id="txtFiltrarNombre" layoutX="154.0" layoutY="25.0" onKeyReleased="#filtrarNombre" />
</children>
</AnchorPane>
The problem you have is that, in the FXML of the view, the table identifier is
tblPersonas
but in the controller (classCRUDEstudianteController
) the name of the only type attributeTableView
istblestudiantes
. Both (id of the table in the FXML and the name of the corresponding attribute in the Controller) have to match for JavaFX to inject the reference to the table in the attribute.Since the above is not true, when calling the
initialize()
controller method, the attributetblestudiantes
isnull
and when trying to manipulate it (doing this:this.tblestudiantes.setItems(estudiantes);
) aNullPointerException
.There are two possible solutions:
tblestudiantes
totblPersonas
, ortblestudiantes