Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

java - Erro NULL CHECK SonarQube

I have an error in the SonarQube of Nullcheck of call when assigning a value to a StorageProcedure.

 call.setBigDecimal(ONE, bank != null ? bank : BigDecimal.ZERO);

Exactly in this excerpt above. He makes the same mistake if he removes the ternary. I did an IF before the function and the same error occurred. enter image description here

 public CarrierReturnDTO getMobileCarriers(BigDecimal bank) throws SQLException {
    CarrierReturnDTO carrierReturnListDTO = new CarrierReturnDTO();
    List<CarrierDTO> mobileCarrierList = new ArrayList<CarrierDTO>();

    DataSource dataSource = jdbcTemplate.getDataSource();
    if (dataSource != null) {
        try (Connection connection = dataSource.getConnection()) {

            if (connection != null) {
                try (CallableStatement call = connection.prepareCall(Constants.COMBO_OPERCEL)) {

                    call.setBigDecimal(ONE, bank != null ? bank : BigDecimal.ZERO);
                    call.registerOutParameter(TWO, OracleTypes.CURSOR);
                    call.execute();

                    ResultSet rs = (ResultSet) call.getObject(TWO);

                    while (rs.next()) {
                        CarrierDTO mobileCarrier = new CarrierDTO();
                        mobileCarrier.setMobileCarrierCode(rs.getBigDecimal(Constants.COD_EMP_OPER));
                        mobileCarrier.setMobileCarrier(rs.getString(Constants.NOM_EMP_OPER));
                        mobileCarrier.setAmountDigitChecked(rs.getBigDecimal(Constants.QTD_DIG_VERIFIC));
                        mobileCarrier.setFlagDoubleDigit(rs.getString(Constants.FLG_DUP_DIGIT));
                        mobileCarrier.setBankCode(rs.getString(Constants.BCO_CNV_ALTAIR));
                        mobileCarrier.setBranchCode(rs.getString(Constants.AGE_CNV_ALTAIR));
                        mobileCarrier.setAccountCode(rs.getString(Constants.CTA_CNV_ALTAIR));

                        mobileCarrierList.add(mobileCarrier);
                    }

                    rs.close();
                }
            }
        }
    }
    carrierReturnListDTO.setCarriers(mobileCarrierList);
    return carrierReturnListDTO;

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have SonarQube's NullCheck enabled. This will give you an error for using a null check in places where a variable cannot be null. Following documentation, this would mean that you are never passing a null into getMobileCarriers(). For robustness of your code, having that null check is a good idea (as you might be using that method from elsewhere, or even externally at some point). Therefore, I would recommend looking into SonarQube's setup and either turning this check off or modifying it such that it won't perform this analysis cross-method calls.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...