我试图通过在新控制器中调用一个函数将数据从一个javafx控制器传递到另一个控制器,但是每次这样做,我都会遇到很多错误,并且大多是NullPointerException
,却不知道为什么。
// admin controller
public class AdminController implements Initializable{
@FXML
private Button newTermButton;
private String error;
private Stage stage;
@Override
public void initialize(URL location, ResourceBundle resources){
}
//buttons
@FXML
public void newTermButton() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("../term/Term.fxml"));
try {
loader.load();
} catch (IOException ex) {
// error
}
TermController tmp = loader.getController();
//error with this line
tmp.newTerm();
javafx.scene.Parent p = loader.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(p));
stage.showAndWait();
}
}
// term controller
public class TermController implements Initializable{
@FXML
private TextField termId;
@FXML
private TextField name;
@FXML
private DatePicker sDate;
@FXML
private DatePicker eDate;
public void loadTerm(int i) {
System.out.println("load term : " + i);
}
public void newTerm() {
System.out.println("new term");
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
}
我正在尝试仅在TermController
的{{1}}中运行一个函数,以便控制器将知道天气,以便从我的数据文件中加载一个术语或创建一个新的术语。