Fix crash on open and close element due to null reference