Net MAUI error object reference not set to an instance of an object when I prepare model data for post api service

I´ve trying to prepapre post model data for save information to a external api, in net maui when i prepare de model ClienteCrm has a field Attributte and Contacts both are chield objects array of the root model.. when i run the program give me this error “object reference not set to an instance of an object” this is the code

                    try
                    {
                  Models.Attribute atributoscl =  new ()
                    {
                        customAttributeId = 1,
                        value = IdenPersonal

                    };

                    Models.Contact contactoscl = new ()
                    {
                        name = NombresCliente + " " + ApellidosCliente,
                        phone = TelefonoCliente,
                        email = CorreoCliente,
                        isBilling = true,
                        isContact = true
                    };
                    ClientesCrm cliente = new()
                    {
                        organizationId = 1,
                        firstName = NombresCliente,
                        lastName = ApellidosCliente,
                        invoiceAddressSameAsContact = true,
                        isLead = false,
                        clientType = 1,
                        note = NotasCliente,
                        attributes = { atributoscl }, ----> if delete this all fine
                        contacts = {contactoscl},    ----> if delete this all fine
                        addressGpsLat = Convert.ToDouble(Latidud),
                        addressGpsLon = Convert.ToDouble(Longitud)


                    };

and this are the models data i use:

                    public class BankAccount
                     {
                public int id { get; set; }
              public string accountNumber { get; set; }
               }
          public class Attribute
           {
       public int id { get; set; }
       public int clientId { get; set; }
       public int customAttributeId { get; set; }
       public string name { get; set; }
       public string key { get; set; }
       public string value { get; set; }
       public bool clientZoneVisible { get; set; }
          }

        public class Contact
           {
       public int id { get; set; }
       public int clientId { get; set; }
       public string email { get; set; }
       public string phone { get; set; }
       public string name { get; set; }
       public bool isBilling { get; set; }
       public bool isContact { get; set; }
       public List<Type> types { get; set; }
         }

      public class ClientesCrm

          {
   public int id { get; set; }
       public string userIdent { get; set; }
       public string previousIsp { get; set; }
       public bool isLead { get; set; }
       public int clientType { get; set; }
       public object companyName { get; set; }
       public object companyRegistrationNumber { get; set; }
       public object companyTaxId { get; set; }
       public object companyWebsite { get; set; }
       public string street1 { get; set; }
       public string street2 { get; set; }
       public string city { get; set; }
       public int? countryId { get; set; }
       public int? stateId { get; set; }
       public string zipCode { get; set; }
       public string fullAddress { get; set; }
       public object invoiceStreet1 { get; set; }
       public object invoiceStreet2 { get; set; }
       public object invoiceCity { get; set; }
       public object invoiceStateId { get; set; }
       public object invoiceCountryId { get; set; }
       public object invoiceZipCode { get; set; }
       public bool invoiceAddressSameAsContact { get; set; }
       public string note { get; set; }
       public object sendInvoiceByPost { get; set; }
       public int? invoiceMaturityDays { get; set; }
       public object stopServiceDue { get; set; }
       public object stopServiceDueDays { get; set; }
       public int organizationId { get; set; }
       public object tax1Id { get; set; }
       public object tax2Id { get; set; }
       public object tax3Id { get; set; }
       public DateTime registrationDate { get; set; }
       public object leadConvertedAt { get; set; }
       public object companyContactFirstName { get; set; }
       public object companyContactLastName { get; set; }
       public bool isActive { get; set; }
       public string firstName { get; set; }
       public string lastName { get; set; }
       public string username { get; set; }
       public List<Contact> contacts { get; set; }
       public List<Attribute> attributes { get; set; }
       public double accountBalance { get; set; }
       public double accountCredit { get; set; }
       public double accountOutstanding { get; set; }
       public string currencyCode { get; set; }
       public string organizationName { get; set; }
       public List<object> bankAccounts { get; set; }
       public List<Tag> tags { get; set; }
       public DateTime? invitationEmailSentDate { get; set; }
       public string avatarColor { get; set; }
       public double? addressGpsLat { get; set; }
       public double? addressGpsLon { get; set; }
       public bool isArchived { get; set; }
       public object generateProformaInvoices { get; set; }
       public bool usesProforma { get; set; }
       public bool hasOverdueInvoice { get; set; }
       public bool hasOutage { get; set; }
       public bool hasSuspendedService { get; set; }
       public bool hasServiceWithoutDevices { get; set; }
       public string referral { get; set; }
       public bool hasPaymentSubscription { get; set; }
       public bool hasAutopayCreditCard { get; set; }
   }

   public class Tag
   {
       public int id { get; set; }
       public string name { get; set; }
       public string colorBackground { get; set; }
       public string colorText { get; set; }
   }

   public class Type
   {
       public int id { get; set; }
         public string name { get; set; }
    }

  • Contacts and attributes need to be instantiated with the new keyword

    – 

object reference not set to an instance of an object

This should tell us that we are declaring a variable but never instantiating it before using it.

From the code you shared, we could find that you declare contacts and attributes in class ClientesCrm, but you didn’t initialize values for them before assigning value for them.

So, you can initialize them before assigning values for them in class ClientesCrm.

You can refer to the following code:

public class ClientesCrm 
{
   // other code

    public List<Contact> contacts { get; set; } =new List<Contact> ();
    public List<Attribute> attributes { get; set; } = new List<Attribute> ();   
}

Leave a Comment