Adding null check to avoid null dereference error