Search code examples
fxmlloader

ComboBox is null on start of ActionEvent


Problem:After the ActionEvent is triggered (selecting an account), the comboBox is null. When I run the program, I can see that the ComboBox is populated. I think I'm doing something wrong with the FXML loader. I read through the following document: https://openjfx.io/javadoc/12/javafx.fxml/javafx/fxml/doc-files/introduction_to_fxml.html#controllers

While thrilling (actually learned a lot), the document did not give me an answer to my problem. Please advise.... Referring me to another source for further education would be appreciated. If you need more information to answer the question, let me know.

Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.ComboBox.setItems(javafx.collections.ObservableList)" because "this.accountComboBox" is null at application.budget/application.budget.Controller.initialize(Controller.java:67)

Line 67 is: accountComboBox.setItems(accountlist);

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);
}

}

Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.ComboBox.setItems(javafx.collections.ObservableList)" because "this.accountComboBox" is null at application.budget/application.budget.Controller.initialize(Controller.java:67)

Line 67 is: accountComboBox.setItems(accountlist);


Solution

  • After some research, it seems like each FXML document needs it's own controller. Been doing this for about 2 weeks. Excuse the lack of education on my part.