Why Objects.requireNonNull() not working in sonar?

Version 6.7.6 (build 38781)

Hi,

In my application I’m trying to get connection from JdbcTemplate,

 try (
      Connection con = Objects.requireNonNull(jdbcTemplate.getDataSource()).getConnection();
        ) {
.....
}

jdbcTemplate.getDataSource() is nullable. This is why I wrap it with Object.requireNonNull() function

    public static <T> T requireNonNull(T obj) {
        if (obj == null)
            throw new NullPointerException();
        return obj;
    }

My problem is when i use this code, I get this error:

But if I change my code to this, I’m not facing any error

        DataSource dataSource = jdbcTemplate.getDataSource();
        if(dataSource == null){
            throw  new NullPointerException("database error");
        }
        try (  Connection con = dataSource.getConnection();)
{

}

Aren’t these 2 codes are same and why am I getting an error in one and not getting the other?

Is this something about my sonarqube settings?

Thanks for helping.

Hi @hasankzl,

Before going to the details, are you able to upgrade your SonarQube to 8.9 LTS or latest version and try again?

Best regards,
Fan

1 Like