[#2212] CID44261:

Added null check to avoid null dereference
[#2222] [#2212] CID43794:

Prevented null dereference, reordered some logic