-1

I am trying to add a contact to my Google contacts account. This is the code:

            // 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();

Authentication and listing the Contact Groups works fine. If I add names or email addresses to the person object I get a Null Reference Exception.

Why?

  • Are you sure the 'Names' property on the 'Person' object has been initialized yet? You may need to do something like 'person.Names = new List();' before you can add to it. – Fraser Aug 02 '21 at 08:49

1 Answers1

0

You can not add to null list you need to build object from EmailAddress

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

and

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

and

request.Contacts = new List<Contact>();
Mena Samer
  • 122
  • 1
  • 11