Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我尝试初始化类时JavaFx调用目标异常_Java_Javafx_Invocationtargetexception - Fatal编程技术网

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

当我尝试初始化类时JavaFx调用目标异常,java,javafx,invocationtargetexception,Java,Javafx,Invocationtargetexception,我对javafx真的很陌生,我想我错过了这里的一些逻辑。My class BankApplication包含实例变量Customer和SavingsAccount。我想初始化它们,这样我的应用程序就可以运行了,上面有客户的名字和ID,还有余额。所以我想创建一个BankApplication实例,为应用程序提供这些信息。这给了我一个错误 这只是代码的一部分 public class BankApplication extends Application implements EventHandl

我对javafx真的很陌生,我想我错过了这里的一些逻辑。My 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"); 
            }
        }
公共类BankApplication扩展了应用程序实现
事件处理程序{
公共静态void main(字符串[]args){
客户Amanda=新客户(“Amanda”,1009);
SavingsAccount AmandaBANK=新的SavingsAccount(Amanda,150);
BankApplication app1=新的银行应用程序(Amanda,AmandaBANK);
发射(args);
}
保护客户;
受保护的储蓄账户;
公共银行应用程序(客户、储蓄账户银行账户){
this.customer=customer;
this.bankAccount=银行账户;
}
私有按钮执行按钮=新按钮(“执行”);
Label customerNameLabel=新标签(“客户名称:+Customer.getName());
Label customerIDLabel=新标签(“客户ID:+Customer.getID());
标签余额标签=新标签(“当前余额:$”+
bankAccount.getBalance()+“);
TextField存款TextField=新的TextField(“存款金额”);
TextField drawintextfield=新的TextField(“提取金额”);
@凌驾
public void start(Stage primaryStage)引发异常{
FlowPane根=新的FlowPane();
场景=新场景(根,400300);
初级阶段。场景(场景);
primaryStage.setTitle(“银行申请”);
executeButton.setOnAction(此);
HBox middle=新的HBox(存款文本字段,取款文本字段);
中段起搏(8);
中间。设置填充(新插图(8));
VBox top=新的VBox(customerNameLabel、customerIDLabel);
VBox bottom=新的VBox(执行按钮、平衡标签);
顶部。设置间距(8);
底部。设置间距(8);
顶部。设置填充(新插图(8));
底部。设置填充(新插图(8));
root.getChildren().add(顶部);
root.getChildren().add(中间);
root.getChildren().add(底部);
primaryStage.show();
}
@凌驾
公共无效句柄(ActionEvent事件){
if(event.getSource()==executeButton){
String depositAmount=depositTextField.getText();
字符串取款金额=取款文本字段。getText();
如果(isDouble(存款金额)=真){
double old_balance=bankAccount.getBalance();
银行账户.存款(双倍.双倍(存款金额));
double new_balance=bankAccount.getBalance();
如果(新余额!=旧余额){
updateBalanceLabel();
dispositextfield.clear();
存款文本字段.setText(“存款金额”);
}
如果(新的余额==旧的余额){
dispositextfield.clear();
存款文本字段.setText(“存款金额”);
}
}
还有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
java.lang.reflect.InvocationTargetException
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源)
在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源)
位于java.lang.reflect.Method.invoke(未知源)
位于com.sun.javafx.application.LaunchImpl.launchApplicationWithArgs(LaunchImpl.java:389)
位于com.sun.javafx.application.LaunchImpl.launchApplication(LaunchImpl.java:328)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源)
在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源)
位于java.lang.reflect.Method.invoke(未知源)
位于sun.launcher.launchelper$FXHelper.main(未知源)
原因:java.lang.NullPointerException
BankAccount.BankApplication.(BankApplication.java:37)
位于BankAccount.BankApplication.main(BankApplication.java:22)
…还有11个
运行应用程序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");
            }
        }
    }
}//...
公共类BankApplication扩展了应用程序实现
事件处理程序{
私人静态客户;
私人静态储蓄账户;
私有按钮执行按钮=新按钮(“执行”);
自有品牌客户标签;
自有品牌平衡标签;
私有文本字段存放文本字段;
私有TextField-TextField;
自有品牌客户标签;
公共静态void main(字符串[]args){
客户=新客户(“Amanda”,1009);
银行账户=新储蓄账户(“Amanda”,150);
应用程序启动(args);
}
公共银行申请书(){
customerNameLabel=新标签(“客户名称:+Customer.getName());
customerIDLabel=新标签(“客户ID:+Customer.getID());
balanceLabel=新标签(“当前余额:$”+
bankAccount.getBalance()+“);
存款文本字段=新文本字段(“存款金额”);
取款文本字段=新文本字段(“取款金额”);
}
@凌驾
public void start(Stage primaryStage)引发异常{
FlowPane根=新的FlowPane();
场景=新场景(根,400,300);
初级阶段。场景(场景);
primaryStage.setTitle(“银行申请”);
executeButton.setOnAction(此);
HBox middle=新的HBox(存款文本字段,取款文本字段);
中段起搏(8);
M
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) {
        // ...
    }

}