Burton
Burton

Reputation: 447

JavaFX setting combobox results in nullpointer

I have a small gui where I select an item in the first combobox which fills a second combobox with data and also changes the text of two labels.

Everything works as expected when I first select the first combobox (combobox_suppliers) and then the second combobox (combobox_radio) and it also updates the two labels (label_rad_substance, label_halftime). But if i then try to select a new option again from the first combobox I get alot of errors and nullpointer exceptions in the log. (The program still continues to function).

The errorlog is quite long but this line seams to be one of the problems:

label_rad_substance.setText(newValue.getSubstance().getName());

This is the code:

package gui;

import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.ResourceBundle;

import dao.RadiopharmaceuticalDao;
import dao.SupplierDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import model.Radiopharmaceutical;
import model.Room;
import model.Supplier;

public class Controller implements Initializable {

    private ObservableList<Supplier> supplierList = FXCollections.observableArrayList();
    private ObservableList<Radiopharmaceutical> radioList = FXCollections.observableArrayList();

    public DatePicker ankomstdatum = new DatePicker();
    public DatePicker kalibreringsdatum = new DatePicker();

    public ComboBox<Supplier> combobox_suppliers = new ComboBox<>();
    public ComboBox<Radiopharmaceutical> combobox_radio = new ComboBox<>();
    public ComboBox<Room> combobox_room = new ComboBox<>();

    public Label label_rad_substance = new Label();
    public Label label_halftime = new Label();

    public TextField text_kalibreringsaktivitet = new TextField();
    public TextField text_kalibreringstid = new TextField();
    public TextField text_batchnr = new TextField();
    public TextField text_kommentar = new TextField();
    public ListView<String> listView = new ListView<String>();

    public CheckBox check_kontamineringskontroll = new CheckBox();
    public Button button = new Button();



    public void addSuppliersToComboBox() {
        supplierList.addAll(new SupplierDao().getAll());
        combobox_suppliers.getItems().addAll(supplierList);
    }






    @Override
    public void initialize(URL location, ResourceBundle resources) {
        addSuppliersToComboBox();
        ankomstdatum.setValue(LocalDate.now());
        combobox_radio.setDisable(true);

        combobox_suppliers.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) ->{
            combobox_radio.getItems().clear();
            combobox_radio.getItems().addAll(FXCollections.observableArrayList(new RadiopharmaceuticalDao().getRadiopharmaceuticalsBySupplierName(newValue.toString())));
            combobox_radio.setDisable(false);
            combobox_radio.getSelectionModel().selectFirst();
        });

        combobox_radio.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue)->{
            label_rad_substance.setText(newValue.getSubstance().getName());
            label_halftime.setText(newValue.getSubstance().getHalfLife()+"");
        });
    }


}

Upvotes: 0

Views: 1583

Answers (1)

fabian
fabian

Reputation: 82461

combobox_radio.getItems().clear();

Removes all items. Items that are not in the items list of a ComboBox are not eligible to be selected. For this reason the statement results in the selected item of combo_radio becoming null which results in a NullPointerException when trying to dereference null in newValue.getSubstance().

You need to add a check for null to fix this, e.g.

combobox_radio.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue)->{
    if (newValue == null) {
        label_rad_substance.setText("");
        label_halftime.setText("");
    } else {
        label_rad_substance.setText(newValue.getSubstance().getName());
        label_halftime.setText(newValue.getSubstance().getHalfLife()+"");
    }
});

BTW: I do not recommend initializing the injected fields. If those fields are properly injected, you created Nodes that are never used. If they are not, you're better of immediately getting an exception that indicates injection does not work instead of having to find out why some change to a Node does not affect the GUI...

Upvotes: 2

Related Questions