do null check before the variable is used
remove unnecessary compared statement
fix resource leak (result of coverity scan 17991, 18057, 18060)