Search code examples
javajavafxtableview

Retrieve only non-null values of a table view rows


I want to save in my database only the rows of non null values of my table view. The problem with TableDowntime.getItems() is that it retrieves all the elements, even the rows with non-selected values, and that cause a problem of NullPointerException

Here's my controller

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

}

Or do you have any other suggestions?


Solution

  • It sounds dumb, but I tried this solution right after "for" clause

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