未将对象引用设置为实例集 - 变量具有值仍然会出现错误

Object reference not set to an instancet - variable has value still error is coming

我收到类似对象引用未设置到对象实例的错误。

但是在调试时我看到值在变量中完美地出现,所以知道为什么仍然会出现这个错误。

请参阅下图和下面的代码。

var vipAppointmentType = await _codeTypeRepository.GetCodeSetsAsQueryable()
                    .FirstOrDefaultAsync(c => c.Id == entity.VIPTypeId);

var dto = ViewVipAppointmentTypeDto.Create(
          entity.Id,
          entity.Code,
          entity.Description,
          vipAppointmentType.Name,
          entity.IsFreeMembership);

dto.VipType.Id = vipAppointmentType.Id;
dto.VipType.Name = vipAppointmentType.Name;
dto.VipType.AbbreviationCode = vipAppointmentType.AbbreviationCode;

dto.VipType为空。初始化VipType如下:

dto.VipType=new VipType(); // add this line
dto.VipType.Id = vipAppointmentType.Id;
dto.VipType.Name = vipAppointmentType.Name;
dto.VipType.AbbreviationCode = vipAppointmentType.AbbreviationCode;