do null check before the variable is used
remove unnecessary compared statement
fix resource leak