如何在JavaFX中使用从另一个控制器发送的整数而不获得NumberFormatException?
我正在使用以下代码将如何在JavaFX中使用从另一个控制器发送的整数而不获得NumberFormatException?,java,javafx,model-view-controller,controller,initialization,Java,Javafx,Model View Controller,Controller,Initialization,我正在使用以下代码将产品发送到另一个控制器: @FXML void onActionModifytProduct(ActionEvent event) throws IOException { Product productSelected = productsTableView.getSelectionModel().getSelectedItem(); FXMLLoader loader = new FXMLLoader();
产品
发送到另一个控制器:
@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()));
}
这就是我在另一个控制器中接收代码的方式:
@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()));
}
现在,我需要在初始化过程中使用发送到
标签idLbl
的整数,但每次我尝试使用它时,都会放入类似int id=integer.parseInt(idLbl.getText())的内容
在public void initialize(URL-URL,ResourceBundle-rb){
下,我得到了一个java.lang.NumberFormatException
,尽管我知道我实际上在传递一个整数,因为我在同一控制器的另一部分使用它(只是不是在初始化期间)。有什么方法可以让我的程序在初始化过程中读取这个整数吗?我已经做了一段时间了,因此非常感谢您的帮助!好的初始化()方法就像您的类中的构造函数,因此它将首先被调用。您正在调用sendProduct()因此,在调用initialize方法期间,您的值仍然为空,这就是它向您抛出数字格式选项的原因。在调用loader.load()
期间,即在调用sendProduct(…)之前,调用initialize()
方法
并设置idLbl
的文本。因此,在调用initialize()
期间,文本为空,您将获得一个NumberFormatException
。为什么不在sendProduct(…)中执行int id=product.getId();
,然后对id
执行任何需要执行的操作?@Jame\u D这绝对有意义。我需要在初始化期间使用它,因为我需要该id来查找部分
,以便在启动时将其加载到表视图中。是否还有其他方法可以发送整数,以便在初始化期间使用它是吗?嗯?在调用了initialize()
之后,您会立即调用sendProduct(…)
。那么,如果您在sendProduct(…)中执行需要执行的操作,那么有什么区别呢
?不清楚你在问什么。非常感谢你,我终于明白了。我太想在初始化下做任何事情了,以至于完全忽略了我可以在sendProduct(…)
。你刚刚让我头疼得要命!我非常感谢你的回答。是否有必要发送一个整数,以便在初始化过程中使用它,还是我必须在第一次打开屏幕时找到另一种查找“部分”的方法?