How can I use an integer I sent from another controller in JavaFX without getting a NumberFormatException?

huangapple go评论76阅读模式
英文:

How can I use an integer I sent from another controller in JavaFX without getting a NumberFormatException?

问题

我使用以下代码将“Product”发送到另一个控制器:

@FXML
void onActionModifytProduct(ActionEvent event) throws IOException {
    Product productSelected = productsTableView.getSelectionModel().getSelectedItem();
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("/view/ModifyProductMenu.fxml"));
    loader.load();

    ModifyProductMenuController MPController = loader.getController();
    MPController.sendProduct(productSelected);

    stage = (Stage) ((Button) event.getSource()).getScene().getWindow();
    Parent scene = loader.getRoot();
    stage.setScene(new Scene(scene));
    stage.show();
}

然后这是我在另一个控制器中接收该代码的方式:

public void sendProduct(Product product) {
    idLbl.setText(String.valueOf(product.getId()));
    modifyProductNameTxt.setText(product.getName());
    modifyProductInvTxt.setText(String.valueOf(product.getStock()));
    modifyProductPriceTxt.setText(String.valueOf(product.getPrice()));
    modifyProductMinTxt.setText(String.valueOf(product.getMin()));
    modifyProductMaxTxt.setText(String.valueOf(product.getMax()));
}

现在我需要在初始化期间使用发送到“Label idLbl”的整数,但每当我尝试在public void initialize(URL url, ResourceBundle rb) 下放置类似 int id = Integer.parseInt(idLbl.getText()); 的内容时,我会收到 java.lang.NumberFormatException,尽管我知道我实际上传递了一个整数,因为我在同一控制器的另一个部分中使用它(只是不在初始化期间使用)。有没有办法让我的程序在初始化期间读取这个整数?我已经做了一段时间了,所以任何帮助都将不胜感激!

英文:

I'm using the following code to send a Product to another controller:

    @FXML
    void onActionModifytProduct(ActionEvent event) throws IOException {
        Product productSelected = productsTableView.getSelectionModel().getSelectedItem();
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/view/ModifyProductMenu.fxml"));
        loader.load();
        
        ModifyProductMenuController MPController = loader.getController();
        MPController.sendProduct(productSelected);
        
        stage = (Stage) ((Button) event.getSource()).getScene().getWindow();
        Parent scene = loader.getRoot();
        stage.setScene(new Scene(scene));
        stage.show();
    }

and then this is how I receive that code in the other controller:

    public void sendProduct(Product product) {
        idLbl.setText(String.valueOf(product.getId()));
        modifyProductNameTxt.setText(product.getName());
        modifyProductInvTxt.setText(String.valueOf(product.getStock()));
        modifyProductPriceTxt.setText(String.valueOf(product.getPrice()));
        modifyProductMinTxt.setText(String.valueOf(product.getMin()));
        modifyProductMaxTxt.setText(String.valueOf(product.getMax()));
    }

Now I need to use the integer that is sent to Label idLbl during the Initialization but everytime I try and use it by putting something like int id = Integer.parseInt(idLbl.getText()); under public void initialize(URL url, ResourceBundle rb) { I get a java.lang.NumberFormatException even though I know I'm actually passing an integer because I use it at another part of the same controller (just not during the initalization). Is there any way I can get my program to read this integer during the initialization? I've been at this for awhile so any help would be really appreciated!

答案1

得分: 0

好的,以下是您要翻译的内容:

初始化方法 initialize() 就像是类中的构造函数,因此它会首先被调用。您在此之后调用了 sendProduct(),因此将值设置给 idLbl 是在此之后发生的。所以,在调用初始化方法时,您的值仍为空,这就是为什么会抛出 NumberFormatException。

英文:

Well initialize() method is like your constructor in your class so it will get called first.You are calling sendProduct() after it so setting the value to idLbl happens after it .So during the invocation of initialize method still your value is empty that's why it throws you an NumberFormatExcpetion.

huangapple
  • 本文由 发表于 2020年10月21日 01:35:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64450538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定