1 回答

TA貢獻1866條經驗 獲得超5個贊
所以有很多錯誤(夸大效果),但你正在學習,所以我會盡力提供幫助。請在復制并粘貼到您的程序之前閱讀全部內容,以便您理解它
從你得到一個空指針的原因開始,你有一個事實,extend DashBoardUserController這意味著DashBoardUserController程序初始化時沒有與你交談TheatresController,當你嘗試訪問它時,它給了你一個空指針,因為沒有初始化在擴展類
所以第 1 步刪除extend DashBoardUserController
因此,既然它已經消失了,我們需要獲取DashBoardUserController對該類的引用,以便它可以調用必要的方法,我們可以在初始化時TheatresController這樣做
FXMLLoader loader = new FXMLLoader(getClass().getResource(resourceLocation));
Pane pane = loader.load();
TheatresController controller = loader.getController();
controller.setDashBoardUserControllerReference(this);//We'll get here
接下來是在中制作必要的方法和變量TheatresController
private DashboardUserController dashboardUserController;//Set at top of class
public void setDashBoardUserControllerReference(DashboardUserController dashboardUserController){
this.dashboardUserController = dashboardUserController;
}
完成這將修復空指針移動以生成您的方法,因為有很多代碼復制對您來說是不必要的輸入
@FXML
public void onjosephclick() {
//weekPickerInput(); //You can delete from here
//addFilmsInList();
//filmList.setShowLocation("Joseph P");
//System.out.println("joseph button pressed");
//try {
// Pane pane = FXMLLoader.load(getClass().getResource("scenes/josephpane.fxml"));
// seatsSelection.getChildren().setAll(pane);
//} catch (IOException e) {
// System.out.println(e);
//}
//setStuff();
//seatsavailable.setText(Integer.toString(seatsJoseph));
//filmPrice = filmList.searchFilm().get().getPrice();
//System.out.println("Price:"+filmPrice); //Down to here
genericOnClick("Joseph P","scenes/josephpane.fxml",seatsJoseph);
//change the strings for each call and remove the unnecessary stuff
//in the methods it will clean it up a lot
}
private void genericOnClick(String location, String resourceLocation, int seats) {
weekPickerInput();
addFilmsInList();
filmList.setShowLocation(location);
System.out.println("Location:"+location);
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource(resourceLocation));
Pane pane = loader.load();
TheatresController controller = loader.getController();
controller.setDashBoardUserControllerReference(this);
seatsSelection.getChildren().setAll(pane);
} catch (IOException e) {
System.out.println(e);
}
setStuff();
seatsavailable.setText(Integer.toString(seats));
filmPrice = filmList.searchFilm().get().getPrice();
System.out.println("Price:"+filmPrice);
}
最終要學習的其他一些東西是 java 命名約定,處理代碼復制
祝好先生好運代碼。如果它不起作用,我可以發布完整的課程,但我認為您應該嘗試將其找出為它的好習慣,我給了您所有您需要的代碼,如果您需要更多幫助,請告訴我
添加回答
舉報