Fix crash by testing NULL pointer