Skip to content

Bugs squashed

David Bürgin edited this page Nov 9, 2015 · 18 revisions

Bugs squashed

There are a few remaining bugs to squash before we can wrap this up.

Missing null checks

The ‘dereferences of possibly-null reference’ in JdbcPetRepositoryImpl and JdbcVisitRepositoryImpl can be dealt with swiftly.

Before:

    private MapSqlParameterSource createVisitParameterSource(Visit visit) {
        return new MapSqlParameterSource()
            .addValue("id", visit.getId())
            .addValue("visit_date", visit.getDate().toDate())
            .addValue("description", visit.getDescription())
            .addValue("pet_id", visit.getPet().getId());
    }

And after:

    private MapSqlParameterSource createVisitParameterSource(Visit visit) {
        DateTime date = visit.getDate();
        Pet pet = visit.getPet();
    
        return new MapSqlParameterSource()
            .addValue("id", visit.getId())
            .addValue("visit_date", date == null ? null : date.toDate())
            .addValue("description", visit.getDescription())
            .addValue("pet_id", pet == null ? null : pet.getId());
    }

Not an improvement to be proud of – a better investment of time might have been to perform proper argument validation everywhere. (Defocusing for a moment it is not hard to see that the entire app doesn’t do particularly robust argument validation.) We commit this in commit 2d795dd and move on. Five to go.

Unboxing of null

Next on the list is a potential ‘unboxing of nullable’ in EntityUtils.

entity.getId() == entityId

What happens when the entity’s ID is null ...

This error is again due to poor argument validation. Still we can make the comparison null safe by boxing the primitive and then using equals instead of ==.

Integer.valueOf(entityId).equals(entity.getId())

Committing this as commit fd848ce.

After another commit 77ad340 (bit tricky this one, can you explain why the fix works?), there is another possible null dereference in PetTypeFormatter.

... another NPE averted

Success

It’s two left and we’re done

...

Finally mvn -Pchecker compile succeeds. After 16 commits we have reached ‘BUILD SUCCESS’.

Are we now bug-free? Some closing thoughts on the next page.

An exercise for the reader. mvn -Pchecker install still does not succeed, because we haven’t checked the test source yet. (mvn compile only compiles the production source.) Try resolving all errors in the tests too.

My solution is in the final three commits, commit aaaaefd, commit a0a076a, and commit 40d765d.

Clone this wiki locally