Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindBugs: Possible null pointer dereference

Tags:

java

findbugs

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?

like image 487
lucian.marcuta Avatar asked Feb 03 '26 17:02

lucian.marcuta


1 Answers

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.

like image 157
Axel Avatar answered Feb 05 '26 06:02

Axel