Tag Archives: stringRedisTemplate.hasKey(xx)

[Solved] IDEA Unboxing of ‘stringRedisTemplate.hasKey(xx)’ may produce ‘NullPointerException’

Question details

Yellow warning when using stringRedisTemplate.hasKey(**)

Warning message: IDEA Unboxing of ‘stringRedisTemplate.hasKey(xx)’ may produce ‘NullPointerException’ warning

Possible null pointer exception when unboxing

Look through the hasKey source code

Looking at the hasKey source code, the hasKey method returns an object wrapper, but using it inside the if condition implicitly unboxes it (ie, converts the result of the call to the original value). If for some reason the hasKey method might return null, an error will occur. For security, you can check if the key exists by following the steps below

solution:

if ( Boolean.TRUE.equals ( redisTemplate.hasKey ( XXXX)) { }

The same is true for other return wrapper types Boolean