Fix possible write to null pointer