我正在调用一个弹出窗口,该窗口的主窗口上的按钮具有 TableView 。我之前在显示“弹出窗口”时遇到问题,但这是因为我设置了错误的路径。我现在需要在弹出窗口中实现TableView。由于需要在弹出窗口中使用某些功能,因此需要将控制器类添加到GenderPopup.fxml文件中。这是我收到错误消息的时候。
我有以下文件:
- Student.java(src / sample / model)
- StudentController.java(src / sample / controller)
- SexDAO.java(src / sample / model)
- StudentView.fxml(src / sample / view)
- GenderPopup.fxml(src / sample / view)
如果TableView位于主窗口中,则效果很好。
// Student.java file
public class Student {
private StringProperty student_id;
private StringProperty first_name;
private StringProperty last_name;
private StringProperty gender_code;
private StringProperty gender_description;
// Constructor
public Student() {
this.student_id = new SimpleStringProperty();
this.first_name = new SimpleStringProperty();
this.last_name = new SimpleStringProperty();
this.gender_code = new SimpleStringProperty();
this.gender_description = new SimpleStringProperty();
}
// Functions for TableView
public void setGenderCode(String genderCode){
this.gender_code.set(genderCode);
}
public StringProperty genderCodeProperty() {
return gender_code;
}
public void setGenderDescription(String genderDescription){
this.gender_description.set(genderDescription);
}
public StringProperty genderDescriptionProperty() {
return gender_description;
}
}
// StudentController.java
public class StudentController {
@FXML private TextField studentIdText;
@FXML private TextField lastNameText;
@FXML private TextField firstNameText;
@FXML private TableView genderTable;
@FXML private TableColumn<Student, String> genderCodeColumn;
@FXML private TableColumn<Student, String> genderDescriptionColumn;
@FXML private ActionEvent actionEvent;
private void initialize () {
// Currently commented as I am getting an Error
// genderCodeColumn.setCellValueFactory(cellData -> cellData.getValue().genderCodeProperty());
// genderDescriptionColumn.setCellValueFactory(cellData -> cellData.getValue().genderDescriptionProperty());
}
@FXML
public void showGenderPopup() throws IOException {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/view/GenderPopup.fxml"));
Parent root = (Parent) fxmlLoader.load();
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
searchAllGender(this.actionEvent);
genderCodeColumn.setCellValueFactory(cellData -> cellData.getValue().genderCodeProperty());
genderDescriptionColumn.setCellValueFactory(cellData -> cellData.getValue().genderDescriptionProperty());
} catch (IOException e)
{
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//Search all Genders
@FXML
private void searchAllGender(ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
this.actionEvent = actionEvent;
try {
//Get all Gender information
ObservableList<Student> genderData = SexDAO.searchAllGender();
//Populate Genders on TableView
populateGenders(genderData);
} catch (SQLException e){
System.out.println("Error occurred while getting Gender information from DB.\n" + e);
throw e;
}
}
// Populate Genders for TableView
@FXML
private void populateGenders (ObservableList<Student> genderData) throws ClassNotFoundException {
//Set items to the genderTable
genderTable.setItems(genderData);
}
// GenderPopup.fxml
// I have removed Height/Width etc as the line was quite long on here
<AnchorPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller.StudentController">
</AnchorPane>
**错误:*
javafx.fxml.LoadException: 在javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) 在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571) 在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 在javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
由以下原因引起:java.lang.reflect.InvocationTargetException
原因:java.lang.NullPointerException