-1

Hi i am not able to bind data in C# class fields.. getting error "Object reference not set to an instance of an object". this is my C# Class as below:

public partial class AccessCodeReqBody
    {
        [JsonProperty("scenarioKey")]
        public string ScenarioKey { get; set; }

        [JsonProperty("destinations")]
        public Destination[] Destinations { get; set; }

        [JsonProperty("whatsApp")]
        public WhatsApp WhatsApp { get; set; }
    }

    public partial class Destination
    {
        [JsonProperty("to")]
        public To To { get; set; }
    }

    public partial class To
    {
        [JsonProperty("phoneNumber")]
        public string PhoneNumber { get; set; }
    }

    public partial class WhatsApp
    {
        [JsonProperty("templateName")]
        public string TemplateName { get; set; }

        [JsonProperty("templateData")]
        public string[] TemplateData { get; set; }

        [JsonProperty("language")]
        public string Language { get; set; }
    }

& the request json as below:

{
   "scenarioKey":"696BDB51C0ACF9E65B86D3E1D08A0084",
   "destinations":[
      {
         "to":{
            "phoneNumber":"919910666888"
         }
      }
   ],
   "whatsApp":{
      "templateName":"access_code",
      "templateData":[
         "Jennifer",
         "Demo",
         "123456"
      ],
      "language":"en"
   }
}

& the code for bind data in c# class as below where i am getting error on binding Phone number & template name :

            AccessCodeReqBody reqbody = new AccessCodeReqBody();
            reqbody.ScenarioKey = "51F5865AE296FAE86614EED";

           

            reqbody.Destinations.To.PhoneNumber = text1;
            reqbody.WhatsApp.TemplateName = "access_code";
            reqbody.WhatsApp.Language = "en";
            reqbody.WhatsApp.TemplateData = GetData(text2.ToString());

Thanks in Advance.

  • No reproduction https://dotnetfiddle.net/xqh4Fl. I have to issue deserializing with your class. May we have more information/context? Your error message looks like a `NullReferenceException` "_Object reference not set to an instance of an object_", so where is the line causing it. And your title is about dynamic, and i see none – Drag and Drop Feb 22 '21 at 10:11
  • you didn't initialise `reqbody.Destination` ? `reqbody.Destination= new Destination[]{};`, you perhaps will need a list of Destination instead of an array if you want to Add. – Drag and Drop Feb 22 '21 at 10:33
  • if anyone has a dupe flag [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/) – Drag and Drop Feb 22 '21 at 10:35

2 Answers2

0

The structure of the classes is correct. use this code

AccessCodeReqBody accessCodeReqBody = JsonConvert.DeserializeObject<AccessCodeReqBody>(json);
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
  • Thanks meysam asadi, But the issue i am getting to bind data in c# class field "Phone Number" & "templatename",i had edited my question for your reference. – Toshan Rajput Feb 22 '21 at 10:27
  • I can not understand what you mean . Do you want to serialize? Or bind a phone number to templatename for what? – Meysam Asadi Feb 22 '21 at 10:33
  • yes i want to bind phone number & template name, after bind the phone number & other data to c# class, i will convert this class to json.. – Toshan Rajput Feb 22 '21 at 10:38
  • Go to array/array element of this answers: https://stackoverflow.com/a/4660186/6560478. It has nothing to do with json. Your Error message is about `NullReferenceException`, and you forgot to initialise stuff. – Drag and Drop Feb 22 '21 at 10:39
0

Seems like a simple enough solution

AccessCodeReqBody reqbody = new AccessCodeReqBody();
reqbody.ScenarioKey = "51F5865AE296FAE86614EED";

// Initialize your WhatsApp object. It is null if you don't
reqbody.WhatsApp = new WhatsApp();
//reqbody.Destinations.To.PhoneNumber = text1;
reqbody.WhatsApp.TemplateName = "access_code";
reqbody.WhatsApp.Language = "en";
reqbody.WhatsApp.TemplateData = GetData(text2.ToString());
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61