c# - Add range is not working with list -
this question has answer here:
- what nullreferenceexception, , how fix it? 29 answers
i having payee contact details object
public class payeecontactdetails { //[jsonproperty("id")] //[defaultvalue("")] //public int id { get; set; } [jsonproperty("contact_name")] [defaultvalue("")] public string contactname { get; set; } [jsonproperty("contact_email")] [defaultvalue("")] public string contactemail { get; set; } ........ ........ }
and here having payeecontactgroup class
public class payeecontactgroup { [jsonproperty("payee_contacts")] public list<payeecontactdetails> payeecontact { get; set; } }
here getting data api response on page page after completion of pages need send data @ time db
for purpose doing this
payeecontactgroup payeecontactdetails = new payeecontactgroup(); var response = httpclient.getasync(uri).result; if (response.issuccessstatuscode) { string data = response.content.readasstringasync().result; var payeecontactgroupdetails = jsonconvert.deserializeobject<payeecontactgroup>(data); if(payeecontactgroupdetails.payeecontact != null && payeecontactgroupdetails.currentpage == 1) { payeecontactdetails.payeecontact = payeecontactgroupdetails.payeecontact.tolist(); } else if(payeecontactgroupdetails.payeecontact != null && payeecontactgroupdetails.currentpage > 1) { payeecontactdetails.payeecontact.addrange(payeecontactgroupdetails.payeecontact); // error @ line } ....... ...... }
but getting error @ line
"payeecontactdetails.payeecontact.addrange(payeecontactgroupdetails.payeecontact);" error : "object reference not set object"
could 1 please on .... many in advance
you need create list
first:
public class payeecontactgroup { [jsonproperty("payee_contacts")] public list<payeecontactdetails> payeecontact { get; set; } = new list<payeecontactdetails>(); }
or create list
when necessary:
payeecontactgroup payeecontactdetails = new payeecontactgroup(); var response = httpclient.getasync(uri).result; if (response.issuccessstatuscode) { string data = response.content.readasstringasync().result; var payeecontactgroupdetails = jsonconvert.deserializeobject<payeecontactgroup>(data); if(payeecontactgroupdetails.payeecontact != null && payeecontactgroupdetails.currentpage == 1) { payeecontactdetails.payeecontact = payeecontactgroupdetails.payeecontact.tolist(); } else if(payeecontactgroupdetails.payeecontact != null && payeecontactgroupdetails.currentpage > 1) { if(payeecontactdetails.payeecontact == null) { payeecontactdetails.payeecontact = new list<payeecontactdetails>(); } payeecontactdetails.payeecontact.addrange(payeecontactgroupdetails.payeecontact); // error @ line } ....... ...... }
Comments
Post a Comment