Hello Im currently creating a JavaFX application for my ATM school assignment but im having trouble passing data between the controllers. I have the Account and Customer class that have getters and setters. In my first controller that controls the Login.fxml im able to assign the setter methods using data from the database. But then when I try to access the same data from different controllers it returns a null.
Below is the first controller
public void LoginButton(javafx.event.ActionEvent actionEvent) throws IOException{
//Get user input
accNumInput = Integer.parseInt(accNumberField.getText());
accPinInput = Integer.parseInt(accPinField.getText());
Conn conn = new Conn();
Account account = new Account();
Customer customer = new Customer();
try{
PreparedStatement stCheckCred = conn.connection.prepareStatement("select * from account where accountnumber = ? and accountpin = ?;");
stCheckCred.setInt(1, accNumInput);
stCheckCred.setInt(2, accPinInput);
ResultSet rsCheckCred = stCheckCred.executeQuery();
if(rsCheckCred.next()){
userAccountNumber = rsCheckCred.getInt("accountnumber");
System.out.println(userAccountNumber);
//Get Customer data
PreparedStatement stGetUserDataCust = conn.connection.prepareStatement("select * from customer where accountnumber = ?");
stGetUserDataCust.setInt(1, userAccountNumber);
ResultSet rsUserDataCust = stGetUserDataCust.executeQuery();
//Get Customer Data from DB
if(rsUserDataCust.next()){
customer.setCustomerId(rsUserDataCust.getInt("customerid"));
customer.setFirstName(rsUserDataCust.getString("customerfirstname"));
customer.setLastName(rsUserDataCust.getString("customerlastname"));
customer.setEmail(rsUserDataCust.getString("customeremail"));
customer.setDateOfBirth(rsUserDataCust.getString("customerdob"));
customer.setGender(rsUserDataCust.getString("customergender"));
customer.setOccupation(rsUserDataCust.getString("customeroccupation"));
customer.setCompanyName(rsUserDataCust.getString("customercompany"));
customer.setCompanyAddress(rsUserDataCust.getString("customeraddress"));
}
//Get Account data from DB
PreparedStatement stGetUserDataAcc = conn.connection.prepareStatement("select * from account where accountnumber = ?");
stGetUserDataAcc.setInt(1, userAccountNumber);
ResultSet rsUserDataAcc = stGetUserDataAcc.executeQuery();
if(rsUserDataAcc.next()){
account.setAccountNumber(rsUserDataAcc.getInt("accountnumber"));
account.setAccountType(rsUserDataAcc.getString("accounttype"));
account.setAccountBalance(rsUserDataAcc.getDouble("accountbalance"));
account.setAccountPin(rsUserDataAcc.getInt("accountpin"));
}
FXMLLoader loaderBal = new FXMLLoader(getClass().getResource("________Balance.fxml"));
root = loaderBal.load();
BalanceController balanceController = loaderBal.getController();
balanceController.GetVarsFromController(customer.getFirstName(), customer.getLastName(), customer.getEmail(), customer.getDateOfBirth(), customer.getGender(),
customer.getOccupation(), customer.getCompanyName(), customer.getCompanyAddress(), account.getAccountNumber(), account.getAccountType(), account.getAccountBalance(), account.getAccountPin());
stage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
catch (SQLException e)
{
System.out.println(e);
}
}
As you can see I set it up so if the user hits the login button then I set the user details to the setter methods in my Customer and Account classes. Then I change the scenes to the Balance page where I want to get the Account Balance. I set up a controller called GetVars from a controller
public void GetVarsFromController(String fn, String ln, String em, String dob, String gen, String occ,
String com, String add, int acc, String accT, double accBal, int accPin) throws IOException {
custFirstName = fn;
custLastNameDB = ln;
custEmailDB = em;
custDobDB = dob;
custGenderDB = gen;
custOccDB = occ;
custCompDB = com;
custCompAddressDB = add;
accNumDB = acc;
accTypeDB = accT;
accBalanceDB = accBal;
accPinDB = accPin;
}
Yes I am able to get the data but when I try to do the same thing for the other controllers such as Withdraw or Statements by either getting data from the Accounts class which I recently set from login or having the Controllers have the same method GetVarsFromController then it returns a null. Any help is appreciated. Thank you.