3
votes

Hi I have the following method:

protected boolean shouldCheckLimit() {
    if (startDate == null || endDate == null) {
        return true;
    }
    final Long currentTime = System.currentTimeMillis();
    if (startDate <= currentTime && currentTime < endDate) {
        return true;
    }
    return false;
}

The problem is that findBugs found the following problem:

Possible null pointer dereference of TimeIntervalLimit.startDate in com.bet.blues.limit.TimeIntervalLimit.shouldCheckLimit() [Scary(8), Normal 

I have to mention that startDate and endDate are Long variables. I tried to add checks for null inside if condition, also I tried to use longValue() method, but with no result. Do you have any idea how can I fix this problem? Could be a bug on fndBugs side?

1
Why don't you send startDate and endDate as parameters? shouldCheckLimit(long startDate, long endDate){} - Uma Kanth
"Could be a bug on fndBugs side?" Unlikely, select isn't broken. I'm having trouble believing you're getting that error with the code above, perhaps you got the error with an earlier version of the code? - T.J. Crowder
Might be a workaround but why don't you use the "long" data type instead of the "Long" object type? - davidgiga1993
(Just wondering, why do people think that "possible null pointer dereference" is a helpful thing to check for? Seems like it adds a lot more noise than signal) - user253751
Your startDate and endDate are global variables. Anything can happen to them (including getting nullify) while getting the currentTime? Try the @UmaKanth suggestion and see if it fixes it. - Rosdi Kasim

1 Answers

5
votes

You get the error because startDate (and also endDate) might be null. But you checked them for null, so that shouldn't be possible, right?

The answer is that both are global and might be set to null at any time by another thread.