add null check on handler.end call