Search code examples
javanullpointerexception

Intellij says that this can be a Null-Pointer. But according javadoc


  private boolean checkStatusAct(Contract contract) {
        if (contract.getActs() == null || contract.getActs().isEmpty()) {
            return true;
        } else if (contract.getActs() != null || (!contract.getActs().isEmpty())) { //here
            for (ContractAct contractAct : contract.getActs()) {
                if (contractAct.getStatusId() == 15) { 
                    return true;
                }
            }
        }
        return false;
    }

Isn't it successively checked one by one and if it's not null (!= null || .isEmpty()) it never produces a NullPoi


Solution

  • In Jav 8 or higher version. Assuming contract.getActs() is a list. You don't need to write else: You can do like

    private boolean checkStatusAct(Contract contract) {
        if (contract.getActs() == null || contract.getActs().isEmpty()) {
            return true;
        }
        return contract.getActs().stream().anyMatch(c -> c.getStatusId() == 15);
    }