Java 标签是否可以由扩展类中的另一个方法修改?

Java 标签是否可以由扩展类中的另一个方法修改?,java,javafx,label,extends,Java,Javafx,Label,Extends,我正在尝试创建一个电影院预订系统,允许用户购买门票和零食,并在每次点击时刷新标签 我有两门课: public class DashboardUserController{ public Label subtotal; public static Double subtotalCounter = 0.0; public static Double filmPrice; @FXML public void onWaterBottleClick(){ subtotalC

我正在尝试创建一个电影院预订系统,允许用户购买门票和零食,并在每次点击时刷新标签

我有两门课:

public class DashboardUserController{

  public Label subtotal;
  public static Double subtotalCounter = 0.0;
  public static Double filmPrice;

  @FXML
  public void onWaterBottleClick(){
    subtotalCounter = subtotalCounter + 1.50;
    orderRecipt = orderRecipt + "Water Bottle 1.50£ \n";
    setSubtotal();
  }
  // and similar methods for each type of snack 

public void setSubtotal(){
    subtotal.setText(subtotalCounter.toString() + " £");
  }

// set the price for each click on the label

  public void addTicket() {
    System.out.println(subtotalCounter);
    subtotalCounter = subtotalCounter + filmPrice;
    setSubtotal();
  } 
}
以及另一个扩展DashboardUserController的类

public class TheatresController extends DashboardUserController {

  @FXML
  private void onClick(MouseEvent event) {                  //method for selection/deselection of seats
    ImageView imageView = (ImageView) event.getSource();
    if (imageView.getImage() == redSeat) {
      Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Do you want to proceed with the unbooking?", ButtonType.YES, ButtonType.NO);
      if (alert.showAndWait().orElse(ButtonType.NO) == ButtonType.YES) {
        imageView.setImage(imageView.getImage() == redSeat ? greenSeat : redSeat);
      }
    } else {
      Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Do you want to proceed with the booking?", ButtonType.YES, ButtonType.NO);
      if (alert.showAndWait().orElse(ButtonType.NO) == ButtonType.YES) {
        imageView.setImage(imageView.getImage() == redSeat ? greenSeat : redSeat);
        addTicket();
      }
    }
  }
现在,如果单击食品按钮,小计计数器和标签将正确刷新:

但是当我点击座位时,电影的价格被正确地加上了,但是标签变成了“空”,给我一个空点例外

奇怪的是,如果我再次点击任何零食,标签会正确显示所选食物的价格加上所选座位的胶片价格

首先,我认为问题出在nullPointException上,所以我检查了这个问题。然后我试图理解扩展类是否可以调用标签并修改它,答案似乎是肯定的,但由于一个可怕的原因,似乎不适用于我的场景

谢谢大家。

所以有很多错误(效果夸张),但你们正在学习,所以我会尽力帮助你们。在复制和粘贴到您的程序之前,请阅读所有内容,以便您理解它

一开始,您获得空指针的原因是您拥有
扩展DashBoardUserController
,这意味着初始化程序的
DashBoardUserController
没有与您的
剧院控制器
通信,当您试图访问它时,它会给您一个空指针因为扩展类中没有初始化任何内容

因此,第1步删除
扩展DashBoardUserController

private DashboardUserController dashboardUserController;//Set at top of class

public void setDashBoardUserControllerReference(DashboardUserController dashboardUserController){
    this.dashboardUserController = dashboardUserController;
}
因此,既然这样做了,我们需要获取该类的
DashBoardUserController
引用,以便它可以调用必要的方法,我们可以在初始化
剧院控制器时这样做

        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命名约定,处理代码复制我看到了大量可以泛化的代码,就像我在上面的方法中向您展示的一样


祝你好运,先生。如果它不起作用,我可以发布完整的课程,但我认为你应该尝试并找出它的良好做法,我给了你所有需要的代码,如果你需要更多帮助,请告诉我,请创建一个。总而言之,这是一个最小的(少量代码)完整的(完全可运行的)可验证的(我可以放入我的IDE并运行无误)示例(重新创建您的问题)。@Matt此代码是一个更大程序的一部分,我如何在这里链接您可以在IDE上运行的最小数量的代码?这意味着重新编写另一个可能与我的问题场景不匹配的代码。我试着把重要的部分,我知道MCVE的指导方针。如果你能对我的问题提出任何改进建议,我将非常乐意修改:)。在这里我可以澄清,通过重新创建您的问题,我的意思是以最小的方式重写必要的代码,以便堆栈溢出上的其他人可以在IDE中运行您的代码。您不需要将代码链接,而是将其嵌入到文章中。您可以这样做,但如果您发现无法一致地重新创建问题,请将代码缩进4个空格还有一个更大的问题是我们看不到的代码(我不想调试整个应用程序),也就是说,也许通过调试程序可以帮助您找到想要解决的问题recreate@Matt我也尝试过调试器,不幸的是我检查了subtotal变量,它只是说它为null,与控制台完全相同。两天以来我一直被困在家里。但是,我会尝试以简洁的方式重新编写代码。是的,很遗憾……如果此答案满足您的问题,请单击问题旁边的复选框“接受”答案,这样其他人就不会认为此问题仍然是开放的。好的,应用刚才的更改,非常感谢您,如果一切顺利,我会立即接受这个问题。非常感谢你的帮助。我仔细阅读了所有你关注的事情。显然,我仍然需要练习,我确信我能够用关键字“Extends”调用这些方法。现在一切都好了。不用担心,从容面对,边走边学,如果你不明白,请寻求帮助