ComboBox 在 ActionEvent 开始时为空

ComboBox is null on start of ActionEvent

问题:ActionEvent触发后(选择账号),comboBox为空。当我 运行 程序时,我可以看到 ComboBox 已填充。我想我在 FXML 加载器上做错了什么。我通读了以下文档:https://openjfx.io/javadoc/12/javafx.fxml/javafx/fxml/doc-files/introduction_to_fxml.html#controllers

虽然激动(其实学到了很多),但是文档并没有给我解答我的问题。请指教....将我推荐给其他来源进行进一步教育将不胜感激。如果您需要更多信息来回答问题,请告诉我。

原因:java.lang.NullPointerException:无法调用“javafx.scene.control.ComboBox.setItems(javafx.collections.ObservableList)”,因为“this.accountComboBox”在应用程序中为空。budget/application。budget.Controller.initialize(Controller.java:67)

第 67 行是:accountComboBox.setItems(帐户列表);

package application.budget;

import datamodel.Account;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import java.io.IOException;
import java.text.NumberFormat;


public class Controller {

    public Button detacctinfo;
    private Stage stage;
    private Scene scene;
    private Parent root;


    @FXML
    private Button createnewacct, Submit;

    @FXML
    private AnchorPane mainPanel;

    @FXML
    private ComboBox<Account> accountComboBox;

    @FXML
    Label acctbalField = new Label();


    private NumberFormat fmt;

public void initialize () {

    ObservableList<Account> accountlist = FXCollections.observableArrayList();
    accountlist.add(new Account("Bank of America", 10010101.32));
    accountlist.add(new Account("Prosperity Bank", 10010101.32));


    //ComboBox<Account> selectacct = new ComboBox<>();

    // Use a StringConverter to configure our ComboBox to display only the film's title
/*    accountComboBox.setConverter(new StringConverter<Account>() {
        @Override
        public String toString(Account account) {
            return account.getAccountName();
        }

        @Override
        public Account fromString(String string) {
            return accountComboBox.getItems().stream().filter(ap ->
                    ap.getAccountName().equals(string)).findFirst().orElse(null);
        }
    });*/

    // Finally, set our ComboBox's items to our sample list
    accountComboBox.setItems(accountlist);

    System.out.println(accountlist);

}

public void OnItemSelected(ActionEvent e ) throws IOException {


    root = FXMLLoader.load(getClass().getResource("AccountHome.fxml"));
    Stage window = (Stage) Submit.getScene().getWindow();
    window.setScene(new Scene(root));


    }

}


package datamodel;




public class Account {

private String accountName;
private Double accountBalance;

public Account(String accountName, Double accountBalance) {
    this.accountName = accountName;
    this.accountBalance = accountBalance;

}

public String getAccountName() {
    return accountName;
}

public void setAccountName(String accountName) {
    this.accountName = accountName;
}

public Double getAccountBalance() {
    return accountBalance;
}

public void setAccountBalance(Double accountBalance) {
    this.accountBalance = accountBalance;
}

@Override //Esto dice que cuando un variable se usa, va a dar el valor de abajo
public String toString() {
    return accountName;
}
        public String getBalance() {
        return String.valueOf(accountBalance);
}

}

原因:java.lang.NullPointerException:无法调用“javafx.scene.control.ComboBox.setItems(javafx.collections.ObservableList)”,因为“this.accountComboBox”为空 在申请时。budget/application.budget.Controller.initialize(Controller.java:67)

第 67 行是:accountComboBox.setItems(帐户列表);

经过一些研究,似乎每个 FXML 文档都需要它自己的控制器。这样做了大约 2 周。请原谅我缺乏教育。