仅检索 table 视图行的非空值

Retrieve only non-null values of a table view rows

我只想在数据库中保存 table 视图的非空值行。 TableDowntime.getItems() 的问题在于它会检索所有元素,甚至是具有未选择值的行,这会导致 NullPointerException

问题

这是我的控制器

private void BtnSaveAction(ActionEvent event) {

    ObservableList<DowntimeDetail> list = TableDowntime.getItems();//we gotta retrieve only rows with non blank cells of the table
    ArrayList<DowntimeDetail> arraylist = new ArrayList<>();
    int i = 0;
    String mat = Tx_Matricule.getText();
    for (DowntimeDetail downtimeDetail : list) {
        if (downtimeDetail.getTypeDT().getSelectionModel().getSelectedItem().toString() != null && downtimeDetail.getDownTime().getText() != null && downtimeDetail.getInterventionTime().getText() != null) {
            if (downtimeDetail.getComment().getText() != null) {
                downtimeDetail.setTypeDtStr(downtimeDetail.getTypeDT().getSelectionModel().getSelectedItem().toString());//get Downtime problem type from tableView
                downtimeDetail.setDtInt(Integer.parseInt(downtimeDetail.getDownTime().getText()));//get Downtime from table view
                downtimeDetail.setItInt(Integer.parseInt(downtimeDetail.getInterventionTime().getText()));//get intervention time from table view
                downtimeDetail.setCommentStr(downtimeDetail.getComment().getText());//get comment from table view
                downtimeDetail.setIdDTP(GetData.getIdDTProblem(downtimeDetail));//set id downtime problem out of downtime problem type
                downtimeDetail.setLqc(mat);//set lqc mat
            }else {
                downtimeDetail.setTypeDtStr(downtimeDetail.getTypeDT().getSelectionModel().getSelectedItem().toString());//get Downtime problem type from tableView
                downtimeDetail.setDtInt(Integer.parseInt(downtimeDetail.getDownTime().getText()));//get Downtime from table view
                downtimeDetail.setItInt(Integer.parseInt(downtimeDetail.getInterventionTime().getText()));//get intervention time from table view
                downtimeDetail.setCommentStr("");//get comment from table view
                downtimeDetail.setIdDTP(GetData.getIdDTProblem(downtimeDetail));//set id downtime problem out of downtime problem type
                downtimeDetail.setLqc(mat);//set lqc mat
            }
        }
        if (downtimeDetail.getTypeDtStr() != null || (downtimeDetail.getDtInt() > 0) || (downtimeDetail.getItInt() > 0)) {
                arraylist.add(downtimeDetail);
        }else {
            LOutput.setStyle("-fx-text-fill:Red");
            LOutput.setText("You must fill all the fields of your desired inputs");
        }
    }

    downtimeDetail.setDowntimes(arraylist);

    int result = GetData.saveDowntimes(downtimeDetail);
    if (result > 0) {
        LOutput.setStyle("-fx-text-fill:Green");
        LOutput.setText(result + " Items were successfully updated");
        Btn_Save.setDisable(true);
    } else {
        LOutput.setStyle("-fx-text-fill:Red");
        LOutput.setText("Error during the update of scrap correction");
        Btn_Save.setDisable(true);
    }

}

或者您还有其他建议吗?

这听起来很蠢,但我在“for”子句之后尝试了这个解决方案

if ((downtimeDetail.getTypeDT().getSelectionModel().getSelectedItem() != null) && (downtimeDetail.getDownTime().getText() != null) && (downtimeDetail.getInterventionTime().getText() != null))