Crash fix: add null check when collapsing elements.