Question

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?

Was it helpful?

Solution

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).

OTHER TIPS

I noticed that you're missing a ; in your code after "boolean status = false", this could be the reason why findbug has problems to parse your code.

OK from what I understood : you want to identify that test has not beeing tested for null. As far as I know there is no way to configure findbugs for doing this. Findbugs can warn you in 2 other cases : - NP_ARGUMENT_MIGHT_BE_NULL : if you call your method saveIrr with a and argument that has not been tested for null before. - NP_NULL_INSTANCEOF : if findbug identified that your value is guaranteed to be null at a point.

You can check all the Null Pointer warnings here they are identified with NP: http://findbugs.sourceforge.net/bugDescriptions.html

I think that such a warning would result in a too huge amount of bugs detected : all methods with arguments would give warnings for arguments that would be used before beeing tested.

What you can do is use the annotions of findbugs/jsr305. So if you add @Nullable to the getDate() method in TestObject it may trigger a NP warning. If you want to use those annotions be sure that the jsr305.jar is in your classpath...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top