If object is an instance variable or a static variable that can be changed from multiple threads, its value can vary between the time you check in the if and the time you call its instance method.
You can change the code to avoid this problem by copying the object to a local variable, for example:
Playable objectCopy = object; if(objectCopy != null) { objectCopy.play(); }
Since objectCopy is a local variable, its value cannot change between the test and the call to play . Of course, the state of the reproduced object may change, but this cannot be fixed by checking null .
source share