【问题标题】:Null reference exception on Person object in Google People API [duplicate]Google People API中Person对象的空引用异常[重复]
【发布时间】:2021-10-07 15:15:12
【问题描述】:

我正在尝试将联系人添加到我的 Google 联系人帐户。这是代码:

            // Create new contact
            Google.Apis.PeopleService.v1.Data.Person person = new Google.Apis.PeopleService.v1.Data.Person();
            Name chef = new Name();
            chef.GivenName = "FirstName";
            chef.FamilyName = "FamilyName";
            EmailAddress email = new EmailAddress();
            email.Type = "work";
            email.Value = "firstname.familyname@something.com";
            person.EmailAddresses.Add(email);

            person.Names.Add(chef);

            ContactToCreate contactToCreate = new ContactToCreate();
            contactToCreate.ContactPerson = person;
            BatchCreateContactsRequest request = new BatchCreateContactsRequest();
            request.Contacts.Add(contactToCreate);
            request.ReadMask = "names";

            BatchCreateContactsResponse reply = service.People.BatchCreateContacts(request).Execute();

身份验证和列出联系人组工作正常。如果我将姓名或电子邮件地址添加到人员对象,我会收到空​​引用异常。

为什么?

【问题讨论】:

  • 你确定'Person'对象的'Names'属性已经被初始化了吗?您可能需要执行类似“person.Names = new List();”的操作在您可以添加之前。

标签: c# google-cloud-platform google-people-api


【解决方案1】:

您不能添加到需要从 EmailAddress 构建对象的空列表中

person.EmailAddresses = new List<EmailAddress>();

person.Names = new List<Name>();

request.Contacts = new List<Contact>();

【讨论】:

  • 正确。这也是我几分钟前发现的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 2021-12-14
  • 2022-01-21
相关资源
最近更新 更多