繁体   English   中英

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

[英]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线程上)。

其结果是:

  1. 您的Application子类中必须有一个无参数的构造函数
  2. 您应该将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM