Joana Ma. Lupe Guarin
Joana Ma. Lupe Guarin

Reputation: 51

SonarQube implies var is not null

In this example below, SonarQube complains that model.toString() is not null and (model == null) is always false, need some help to understand what can be done to fix it. because the bookmark is initialized as a variable within the if statement and apparently will be null.

public static class Mapper implements DataStore.ModelMapper<Membership, 
MembershipPassDTO> {            
    @Override
    public MembershipPassDTO mapModel(Membership model) {
        VALogger.e("SULOD Membership Mapper", "" + model.toString());

        if (model == null) {
            return new MembershipPassDTO(model, "", "", "", "", "", "");
        }
        return new MembershipPassDTO(model, model.getVitalityMembershipId(), model.getMembershipNumber(), model.getCustomerNumber(),
                model.getVitalityStatus(), model.getMembershipStartDate(), model.getMembershipStatus());
    }
}

Upvotes: 5

Views: 1631

Answers (1)

Martin Frank
Martin Frank

Reputation: 3454

whenever you call model.toString() and your model is null indeed, then your method will throw a NullPoiunterException.

but if model.toString() did not throw a NullPointerEcxeption then it's obvious, that model == null is always false...

well - that's what SonarQube is trying to tell you...


if you want to get rid of the warning you can do what jensgram suggested:

VALogger.e("SULOD Membership Mapper", ""+model); //implicitly calls toString()

Upvotes: 2

Related Questions