Update All I want to do is just save the value of the label to another class. This is my biggest problem and nothing seems to work.
I've been working on a javafx project for a few weeks. I was almost done but ran into trouble communicating information amoung classes. After 2 days, I still cant figure out why it wont work. Here is some code below:
Class where I initialize data:
@FXML
Label money;
@FXML
int holder;
@FXML
Label tst;
int currency;
BankData bank;
//this is where text for money label is set from user input
public void presetMoney(ActionEvent e) {
if(e.getSource() == ten) {
currency += 10;
money.setText(Integer.toString(currency));
}
//this is where I try to retrieve the value of money
public void testing() {
this.holder = Integer.parseInt(money.getText());
bank.storeData(holder);
tst.setText(Integer.toString(holder));
}
public int getTesting() {
return this.holder;
}
BankData.java (Class where I'm trying to set the data):
String data;
public void storeData(String data) {
this.data = data;
}
But of course, I get this error:
Caused by: java.lang.NullPointerException
at application.TransactionScreenController.testing(TransactionScreenController.java:159)
... 58 more
Before I get redirected to some NullPointer post and get downvoted, I just wanted to know why my variable returns null when attempting to save it to another class. Clearly the variable "holder" is not null since I tested it by setting it to another label.
Here is a picture to help make it more clear
I successfully got text from the Label money and set it to int holder. Then I set the value of holder to the label tst. However, when I try to set the value of holder to another class, I get a nullpointer.
EDIT I initialized BankData bank = new BankData() and tried changing the variables to int. I just get 0 instead of null.
EDIT 2 So I tried FXML injection and now I get a class exception
public void trasComplete(ActionEvent e) throws IOException {
FXMLLoader loader = new FXMLLoader();
//methods to change screen not included because of irrelevance
BankData n = loader.getController();
n.storeData(holder);
}
Caused by: java.lang.ClassCastException: application.SecrurityScreenController cannot be cast to application.BankData
at application.TransactionScreenController.trasComplete(TransactionScreenController.java:173)
... 58 more