Issue
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!
Solution
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.
Answered By - Hasindu Dahanayake