Sonar issues update

added null check, and moved lock out side try block