JavaFX设置组合框导致空指针

时间:2018-12-07 10:42:15

标签: java javafx

我有一个小gui,可以在第一个组合框中选择一个项目,该项目将在第二个组合框中填充数据,并更改两个标签的文本。

当我首先选择第一个组合框(combobox_suppliers),然后选择第二个组合框(combobox_radio)时,一切都按预期进行,并且还更新了两个标签(label_rad_substance,label_halftime)。但是,如果我随后尝试从第一个组合框中再次选择一个新选项,则会在日志中收到很多错误和nullpointer异常。 (程序仍然继续运行。)

错误日志很长,但此行似乎是问题之一:

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

这是代码:

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()+"");
        });
    }


}

1 个答案:

答案 0 :(得分:2)

combobox_radio.getItems().clear();

删除所有项目。 items的{​​{1}}列表中未包含的项目无法选择。因此,该语句导致ComboBox的选定项变为combo_radio,当尝试在null中取消引用NullPointerException时导致null

您需要添加newValue.getSubstance()的支票以解决此问题,例如

null

顺便说一句:我不建议初始化注入的字段。如果正确注入了这些字段,那么您将创建从未使用过的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()+""); } }); 。如果不是,那么最好立即获得一个指示注入不起作用的异常,而不必找出为什么对Node进行的某些更改不会影响GUI ...