[英]JavaFx invocation target exception when I try to initialize class
我對javafx真的很陌生,我想我在這里缺少一些邏輯。 我的班級BankApplication包含實例變量Customer和SavingsAccount。 我想初始化它們,以便我的應用程序運行,客戶名稱和ID在頂部,以及余額。 因此,我想創建一個BankApplication實例,以向應用提供此信息。 它給了我一個錯誤。
這只是代碼的一部分。
public class BankApplication extends Application implements
EventHandler<ActionEvent>{
public static void main(String[] args) {
Customer Amanda = new Customer("Amanda" , 1009);
SavingsAccount AmandaBANK = new SavingsAccount(Amanda , 150);
BankApplication app1 = new BankApplication(Amanda,AmandaBANK);
launch(args);
}
protected Customer customer;
protected SavingsAccount bankAccount;
public BankApplication(Customer customer, SavingsAccount bankAccount) {
this.customer = customer;
this.bankAccount = bankAccount;
}
private Button executeButton = new Button("Execute");
Label customerNameLabel = new Label("Customer name: " + customer.getName());
Label customerIDLabel = new Label("Customer ID: "+ customer.getID());
Label balanceLabel = new Label("Current balance: $" +
bankAccount.getBalance() + ".");
TextField depositTextField = new TextField("Amt to deposit");
TextField withdrawTextField = new TextField("Amt to withdraw");
@Override
public void start(Stage primaryStage) throws Exception {
FlowPane root = new FlowPane();
Scene scene = new Scene(root,400,300);
primaryStage.setScene(scene);
primaryStage.setTitle("Bank application");
executeButton.setOnAction(this);
HBox middle = new HBox(depositTextField,withdrawTextField);
middle.setSpacing(8);
middle.setPadding(new Insets(8));
VBox top = new VBox(customerNameLabel,customerIDLabel);
VBox bottom = new VBox(executeButton,balanceLabel);
top.setSpacing(8);
bottom.setSpacing(8);
top.setPadding(new Insets(8));
bottom.setPadding(new Insets(8));
root.getChildren().add(top);
root.getChildren().add(middle);
root.getChildren().add(bottom);
primaryStage.show();
}
@Override
public void handle(ActionEvent event) {
if (event.getSource() == executeButton) {
String depositAmount = depositTextField.getText();
String withdrawAmount = withdrawTextField.getText();
if (isDouble(depositAmount) == true) {
double old_balance = bankAccount.getBalance();
bankAccount.deposit(Double.parseDouble(depositAmount));
double new_balance = bankAccount.getBalance();
if (new_balance != old_balance) {
updateBalanceLabel();
depositTextField.clear();
depositTextField.setText("Amt to deposit");
}
else if (new_balance == old_balance) {
depositTextField.clear();
depositTextField.setText("Amt to deposit");
}
}
和javafx錯誤。
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.NullPointerException
at BankAccount.BankApplication.<init>(BankApplication.java:37)
at BankAccount.BankApplication.main(BankApplication.java:22)
... 11 more
Exception running application BankAccount.BankApplication
您可以根據傳遞給構造函數的客戶和帳戶初始化幾個實例變量(customerIDLabel,customerIDLabel)。 問題是,java在字段初始化塊之后調用構造函數。 這很容易解決:只需將字段初始化代碼移到構造函數中:
protected Customer customer;
protected SavingsAccount bankAccount;
private Button executeButton = new Button("Execute");
private Label customerIDLabel;
private Label balanceLabel;
private TextField depositTextField;
private TextField withdrawTextField;
private Label customerNameLabel;
public BankApplication(Customer customer, SavingsAccount bankAccount) {
this.customer = customer;
this.bankAccount = bankAccount;
customerNameLabel = new Label("Customer name: " + customer.getName());
customerIDLabel = new Label("Customer ID: " + customer.getID());
balanceLabel = new Label("Current balance: $" +
bankAccount.getBalance() + ".");
depositTextField = new TextField("Amt to deposit");
withdrawTextField = new TextField("Amt to withdraw");
}
這將修復您的NullPointerException,但是您是否注意到app1被忽略了? 在JavaFX中,不應創建應用程序的實例。 相反,您必須讓JavaFx運行時為您執行此操作。 在這里一般認為使用靜態是可以接受的。
public class BankApplication extends Application implements
EventHandler<ActionEvent> {
private static Customer customer;
private static SavingsAccount bankAccount;
private Button executeButton = new Button("Execute");
private Label customerIDLabel;
private Label balanceLabel;
private TextField depositTextField;
private TextField withdrawTextField;
private Label customerNameLabel;
public static void main(String[] args) {
customer = new Customer("Amanda", 1009);
bankAccount = new SavingsAccount("Amanda", 150);
Application.launch(args);
}
public BankApplication() {
customerNameLabel = new Label("Customer name: " + customer.getName());
customerIDLabel = new Label("Customer ID: " + customer.getID());
balanceLabel = new Label("Current balance: $" +
bankAccount.getBalance() + ".");
depositTextField = new TextField("Amt to deposit");
withdrawTextField = new TextField("Amt to withdraw");
}
@Override
public void start(Stage primaryStage) throws Exception {
FlowPane root = new FlowPane();
Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("Bank application");
executeButton.setOnAction(this);
HBox middle = new HBox(depositTextField, withdrawTextField);
middle.setSpacing(8);
middle.setPadding(new Insets(8));
VBox top = new VBox(customerNameLabel, customerIDLabel);
VBox bottom = new VBox(executeButton, balanceLabel);
top.setSpacing(8);
bottom.setSpacing(8);
top.setPadding(new Insets(8));
bottom.setPadding(new Insets(8));
root.getChildren().add(top);
root.getChildren().add(middle);
root.getChildren().add(bottom);
primaryStage.show();
}
@Override
public void handle(ActionEvent event) {
if (event.getSource() == executeButton) {
String depositAmount = depositTextField.getText();
String withdrawAmount = withdrawTextField.getText();
if (isDouble(depositAmount) == true) {
double old_balance = bankAccount.getBalance();
bankAccount.deposit(Double.parseDouble(depositAmount));
double new_balance = bankAccount.getBalance();
if (new_balance != old_balance) {
updateBalanceLabel();
depositTextField.clear();
depositTextField.setText("Amt to deposit");
} else if (new_balance == old_balance) {
depositTextField.clear();
depositTextField.setText("Amt to deposit");
}
}
}
}//...
當啟動JavaFX應用程序時(通過調用Application.launch()
或直接由系統啟動),許多事情會“自動”發生。
其中之一是通過調用無參數構造函數來創建Application
子類的實例。
發生的另一件事是在創建的實例上調用了start()
方法。 (這發生在FX Application線程上)。
其結果是:
Application
子類中必須有一個無參數的構造函數 start()
方法視為應用程序的入口點; 即發生的第一件事。 任何初始化都應在此處完成。 您通常應該避免在main()
方法中除了調用launch()
。 因此,您可以將應用程序類重構為:
public class BankApplication extends Application implements EventHandler<ActionEvent>{
public static void main(String[] args) {
launch(args);
}
protected Customer customer;
protected SavingsAccount bankAccount;
private Button executeButton ;
private Label customerNameLabel ;
private Label customerIDLabel ;
private Label balanceLabel ;
private TextField depositTextField ;
private TextField withdrawTextField ;
@Override
public void start(Stage primaryStage) throws Exception {
Customer customer = new Customer("Amanda" , 1009);
SavingsAccount bankAccount = new SavingsAccount(Amanda , 150);
executeButton = new Button("Execute");
customerNameLabel = new Label("Customer name: " + customer.getName());
customerIDLabel = new Label("Customer ID: "+ customer.getID());
balanceLabel = new Label("Current balance: $" +
bankAccount.getBalance() + ".");
depositTextField = new TextField("Amt to deposit");
withdrawTextField = new TextField("Amt to withdraw");
FlowPane root = new FlowPane();
Scene scene = new Scene(root,400,300);
primaryStage.setScene(scene);
primaryStage.setTitle("Bank application");
executeButton.setOnAction(this);
HBox middle = new HBox(depositTextField,withdrawTextField);
middle.setSpacing(8);
middle.setPadding(new Insets(8));
VBox top = new VBox(customerNameLabel,customerIDLabel);
VBox bottom = new VBox(executeButton,balanceLabel);
top.setSpacing(8);
bottom.setSpacing(8);
top.setPadding(new Insets(8));
bottom.setPadding(new Insets(8));
root.getChildren().add(top);
root.getChildren().add(middle);
root.getChildren().add(bottom);
primaryStage.show();
}
@Override
public void handle(ActionEvent event) {
// ...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.