Google 人 API 中 Person 对象的空引用异常

Null reference exception on Person object in Google People API

我正在尝试将联系人添加到我的 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();

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

为什么?

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

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

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

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