Fix possible null reference exceptions