Skip to content
Advertisement

Sonar: Possible null pointer dereference due to return value of called method

I am getting issue from Sonar: “Style – Possible null pointer dereference due to return value of called method. findbugs:NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE. the issue is on lockUntil.after(new Date());

try {
            String sql = "select lock_until from shedlock where NAME=?";
            Timestamp lockUntil = jdbcTemplate.queryForObject(sql, new Object[] {taskname}, Timestamp.class);
            
            return lockUntil.after(new Date()); //issue line
            
        } catch (EmptyResultDataAccessException e){
            LOGGER.info("Checking shedlock for locked task[" + taskname + "]. No task exists. Exception: " + e.getLocalizedMessage());
            return false;
        }

I’ve tried to change this issue line with

Date date = new Date();
return lockUntil.after(date);

or

Date date = new Date();
if(date != null)
    return lockUntil.after(date);
else
    return false;  -> this line is dead end

But it introduces other issues.

Can anyone please guide?

Advertisement

Answer

Assuming that new Date() will never return null should be a valid assumption. My guess is that the possibly null value Sonar is complaining about is lockUntil. I assume that queryForObject will return null if the query does not match any rows, so it makes sense that you’d get a complaint about using lockUntil without first checking if it is null.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement