Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FIndbug not identifying null pointer exception

I am using Findbugs integerated with eclipse.

When I run findbugs on my project the below code is not captured for possible null pointer exception.

In the below snippet the object test is prone to null pointer exception which is not identified by findbugs.

@Override
    public boolean saveIrr(TestObject test) throws DuplicateRecordException {
        boolean status = false
        try {
            test.getAddDate();
            status = adhocMaintPopupMapper.saveIrr(IRRPopupMaint);
        } catch (DataIntegrityViolationException e) {
            logger.error("Error in Saving!", e);
            throw new TransactionDataException("Error in Saving!", e);
        }
        return status;
    }

Is there any configuration change required to make findbugs to identify this?

like image 403
prabu Avatar asked Mar 03 '26 14:03

prabu


1 Answers

If you add @Nonnull to the parameter declaration, FindBugs will highlight anywhere you're passing a value that isn't checked for null. If you mark it @CheckForNull, FindBugs will highlight anywhere in the method that you access it without checking for null.

Which you do depends on the method's contract: does it tolerate null or not? Looking at its implementation it does not allow for null without throwing an unexpected exception. Therefore, test should be marked @Nonnull so you can spot incorrect calls.

Update

FindBugs will only check fields, parameters, method return values that are annotated with either @Nonnull or @CheckForNull. Anything without an annotation is assumed @Nullable which tells FindBugs to ignore it.

public boolean saveIrr(@Nonnull TestObject test) { ... }

public void dontCareAboutNull(TestObject value) {
    saveIrr(value); // no bug
}

public void mightBeNull(@CheckForNull TestObject value) {
    saveIrr(value); // bug
}

For this reason, we apply @Nonnull to all three types of values at the package level. Any value that needs to allow null must be annotated with @CheckForNull. We do not allow the use of @Nullable except in very few corner cases (e.g. @Autowired fields which Spring enforces).

like image 145
David Harkness Avatar answered Mar 05 '26 02:03

David Harkness



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!