gpt4 book ai didi

当我尝试初始化类时 JavaFx 调用目标异常

转载 作者:行者123 更新时间:2023-11-30 07:48:38 28 4
gpt4 key购买 nike

我真的是 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 在字段初始化 block 之后 调用您的构造函数。这很容易修复:只需将字段初始化代码移动到您的构造函数中:

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 调用目标异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49000783/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com