当我尝试初始化时 JavaFx 调用目标异常 class
JavaFx invocation target exception when I try to initialize class
我是 javafx 的新手,我想我在这里遗漏了一些逻辑。我的 class 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()
,或直接由系统启动),会发生许多事情 "automatically"。
其中之一是您的 Application
subclass 的实例是通过调用 no-argument 构造函数创建的。
发生的另一件事是在创建的实例上调用 start()
方法。 (这发生在 FX 应用程序线程上)。
这样做的结果是:
- 您的
Application
子class 中必须有一个 no-argument 构造函数
- 您应该将
start()
方法视为应用程序的入口点;即发生的第一件事。任何初始化都应该在这里完成。你应该(通常)避免在 main()
方法中有任何东西,除了调用 launch()
.
因此您可以将您的应用程序 class 重组为:
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) {
// ...
}
}
我是 javafx 的新手,我想我在这里遗漏了一些逻辑。我的 class 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()
,或直接由系统启动),会发生许多事情 "automatically"。
其中之一是您的 Application
subclass 的实例是通过调用 no-argument 构造函数创建的。
发生的另一件事是在创建的实例上调用 start()
方法。 (这发生在 FX 应用程序线程上)。
这样做的结果是:
- 您的
Application
子class 中必须有一个 no-argument 构造函数
- 您应该将
start()
方法视为应用程序的入口点;即发生的第一件事。任何初始化都应该在这里完成。你应该(通常)避免在main()
方法中有任何东西,除了调用launch()
.
因此您可以将您的应用程序 class 重组为:
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) {
// ...
}
}