Menu

#28 false positive when using Objects.isNull inside a try with resources

3.0.1
open
nobody
None
5
2017-04-26
2017-04-26
No

Hello,

I found a piece of code that is reported as a false positive "Possible null pointer dereference" when checked with Objects.isNull. Curiously It only fails inside a try-with resources:

This fails for me for object 'nullable'

 public void findbugs() throws Exception {
        try (Closeable b = () -> {
            LOG.info("close");
        }) {
            String nullable = getString();
            if (Objects.isNull(nullable)) {
                throw new ServletException("aaa");
            }
            LOG.warn(nullable.toString());

        } catch (Exception e) {
            LOG.error("eeee", e);
        }

    }

    @CheckForNull
    public String getString() {
         return (int) (Math.random() * 1000) % 2 == 0 ? "a" : null;
    }

but using a simple try works fine:

  public void findbugs() throws Exception {
        try {
            String nullable = getString();
            if (Objects.isNull(nullable)) {
                throw new ServletException("aaa");
            }
            LOG.warn(nullable.toString());

        } catch (Exception e) {
            LOG.error("eeee", e);
        }

    }

    @CheckForNull
    public String getString() {
         return (int) (Math.random() * 1000) % 2 == 0 ? "a" : null;
    }

replacing Objects.isNull for a regular null check also works fine.

This happens using:
findbugs 3.0.1 with the maven plugin 3.0.4
java 1.8.0_65-b17

Greetings.

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.