-2

I am having payee contact details object like this

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 i am having PayeeContactGroup class like this

 public class PayeeContactGroup
 {   
    [JsonProperty("payee_contacts")]    
    public List<PayeeContactDetails> PayeeContact { get; set; }

 }

here i am getting data from api response on page by page after completion of all pages i need to send all data at a time to DB

for this purpose i am doing like 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 at this line 
      }
      .......
      ......
  }         

But i am getting error at this line

"payeeContactDetails.PayeeContact.AddRange(payeeContactGroupDetails.PayeeContact);" Error : "Object reference not set to an object"

Could any one please help on this .... Many thanks in advance

Glory Raj
  • 17,397
  • 27
  • 100
  • 203

2 Answers2

4

You need to create the List first:

public class PayeeContactGroup
 {   
    [JsonProperty("payee_contacts")]    
    public List<PayeeContactDetails> PayeeContact { get; set; } = new List<PayeeContactDetails>();

 }

Or create the List only 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 at this line 
    }
      .......
      ......
} 
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
  • 1
    That usually is done when JSON deserializes the data. – Ian H. Jul 25 '17 at 10:19
  • @IanH. Even if there was no data for that List? – Romano Zumbé Jul 25 '17 at 10:20
  • Got confused by his code, my bad, you are right, the object is never assigned to a deserialized JSON object. – Ian H. Jul 25 '17 at 10:24
  • i need to keep already existing list that i have got in first page .. but when i implemented your code i am not able to get already added list object in first page .. – Glory Raj Jul 25 '17 at 10:26
  • @EnigmaState What is first page? – Romano Zumbé Jul 25 '17 at 10:27
  • i am getting response object from api on page basis (i.e) first page i am getting one object and second time i am calling same api with query params as page number 2 and getting results from api for second page – Glory Raj Jul 25 '17 at 10:28
  • @EnigmaState That's a completely different question. Your null reference issues is now solved, I would have hoped that someone with over 12k rep would know that by now. – DavidG Jul 25 '17 at 10:29
  • who told it is null reference exception .. i am getting "Object reference not set to an object" – Glory Raj Jul 25 '17 at 10:30
  • @EnigmaState That's literally what a NRE is! The full exception will say `NullReferenceException: Object reference not set to an instance of an object.` – DavidG Jul 25 '17 at 10:31
  • @EnigmaState I've edited my answer. This way, there will only be a List generated if you want to add items to it, but it is not present. The changes from the original answer must not be implemented then – Romano Zumbé Jul 25 '17 at 10:32
  • hmm the problem is i need to append the new objects to already existing list .. as i am getting response from api on page by page basis.. – Glory Raj Jul 25 '17 at 10:34
  • @EnigmaState Like I said, that is a different question completely, please don't do that. – DavidG Jul 25 '17 at 10:34
  • With my edited answer this should work. If there is already a List assigned to the object the new entries will be appended, if there is none, it will be created first – Romano Zumbé Jul 25 '17 at 10:35
  • do u want me keep this line ..payeeContactDetails.PayeeContact.AddRange(payeeContactGroupDetails.PayeeContact); – Glory Raj Jul 25 '17 at 10:35
  • Yes. There was an error in my answer. A bracket before that line. I removed it – Romano Zumbé Jul 25 '17 at 10:36
  • yeah got it its working many thanks ... – Glory Raj Jul 25 '17 at 10:39
1

Initialise the empty list first before adding. Or the AddRange method is trying to add to null. This could be done in code:

PayeeContactGroup payeeContactDetails = new PayeeContactGroup();

payeeContactDetails.PayeeContact = new List<PayeeContactDetails>();
DavidG
  • 113,891
  • 12
  • 217
  • 223
Wilcko
  • 19
  • 2