Nullcheck of status at line 114 of value previously dereferenced in Sonar

Viewed 1275

For the below piece of code Sonar throws me a critical violation - Correctness - Nullcheck of status value previously dereferenced

Can someone suggest on this on what am I doing wrong here?

code

public boolean isExactMacthBill(AddressResponse response) {
        boolean exactMatch = false;
        if (null != response && null != response.getHostResponse()) {
            HostResponseDetail hostResponse = response.getHostResponse();
            String addressStatus = hostResponse.getMatchStatus();
            ResponseDetail status = hostResponse.getStatus();
            String addressMatchCode = status.getCode();
            if (null != response.getMatchedAddresses() && response.getMatchedAddresses().size() > 0 && status != null) {
                if (addressStatus.equalsIgnoreCase(Constants.USPS_MATCH)
                        || (addressStatus.equalsIgnoreCase(Constants.PARTIAL_MATCH)
                                && addressMatchCode.equalsIgnoreCase("3SXU"))) {
                    exactMatch = true;
                } else
                    exactMatch = false;
            }
        }
        return exactMatch;
    }

enter image description here

3 Answers

The actual problem is in the line after the highlighted one - you've got:

if (... && status != null)

Just remove that check and I think SonarLint will be happy. It unnecessary, because if status is null then status.getCode() will already have thrown an exception before you reach that condition.

Fundamentally, you need to know whether getStatus() should ever return null - whether you have to handle that situation explicitly. If you do, you should check it before your call to status.getCode(), and react accordingly. If you don't, it's fine to call the getCode() method - if your assumption is incorrect, you'll get a NullPointerException as normal, which is probably the most appropriate result for the scenario of "the world isn't as I expect it to be". But you shouldn't try to "handle" it being null after you've already depended on it being non-null.

status can be null when it is received from hostResponse.getStatus();; so when the line String addressMatchCode = status.getCode(); is called it can result in a Null Reference Exception.

You should verify all the variables if there are null before calling methods on them.

Move your addressMatchCode inside your if condition which null check the status.

public boolean isExactMacthBill(AddressResponse response) {
    boolean exactMatch = false;
    if (null != response && null != response.getHostResponse()) {
        HostResponseDetail hostResponse = response.getHostResponse();
        String addressStatus = hostResponse.getMatchStatus();
        ResponseDetail status = hostResponse.getStatus();

        if (null != response.getMatchedAddresses() && response.getMatchedAddresses().size() > 0 && status != null) {
            String addressMatchCode = status.getCode();
            if (addressStatus.equalsIgnoreCase(Constants.USPS_MATCH)
                    || (addressStatus.equalsIgnoreCase(Constants.PARTIAL_MATCH)
                            && addressMatchCode.equalsIgnoreCase("3SXU"))) {
                exactMatch = true;
            } else
                exactMatch = false;
        }
    }
    return exactMatch;
}
Related