简体   繁体   中英

How to initialize an array in a property's sub-class? (Object reference not set… error)

First of all, please stick to how I accomplish this with the current setup, and not in trying to redesign how it is structured. I have to build an app/web service to receive a JSON query. The JSON is generated by a widget that we can not customize. I used Visual Studio to create the class structure from a JSON example.

I want to test my service, and I'm getting errors in SOAPUI, so I need something where I can debug, so I'm hard-coding the JSON input object for my Service Reference.

Here are the classes, as they reside on the web service -

[DataContract]
    public class QuoterIn
   {
        [DataMember]
        public Quoterinput quoterinput { get; set; }
    }

    public class Quoterinput
    {

        public string agent_id { get; set; }
        public string key { get; set; }
        public string auto_assign { get; set; }
        public Consumer consumer { get; set; }
        public string viaEmail { get; set; }
    }

    public class Consumer
    {
        public string brand_id { get; set; }
        public string full_name { get; set; }
        public Addresses_Attributes[] addresses_attributes { get; set; }
        public string birth_or_trust_date { get; set; }
        public string gender { get; set; }
        public Emails_Attributes[] emails_attributes { get; set; }
        public Phones_Attributes[] phones_attributes { get; set; }
        public Cases_Attributes[] cases_attributes { get; set; }
    }

    public class Addresses_Attributes
    {
        public string zip { get; set; }
    }

    public class Emails_Attributes
    {
        public string value { get; set; }
    }

    public class Phones_Attributes
    {
        public string value { get; set; }
    }

    public class Cases_Attributes
    {
        public Quoting_Details_Attributes[] quoting_details_attributes { get; set; }
    }

    public class Quoting_Details_Attributes
    {
        public string carrier_id { get; set; }
        public string duration_id { get; set; }
        public string policy_type_id { get; set; }
        public string plan_name { get; set; }
        public string product_type_name { get; set; }
        public string face_amount { get; set; }
        public string carrier_health_class { get; set; }
        public string planned_modal_premium { get; set; }
        public string premium_mode_id { get; set; }
        public string health_class_id { get; set; }
    }

Looking at the sub-class of Consumer, there are several sub-classes that are arrays - address_attributes, phone_attributes, and email_attributes.

I'm able to initialize the main JSON input/inquiry container, and then I can initialize the main class and the sub-class of "Consumer." I can enter data for the main fields, and then fields in the sub-class of Consumer, but I can't initialize the arrays.

Here's the code up until this point for populating that input container -

    protected void btnRun_Click(object sender, EventArgs e)
    {
        QuoterIn req = new QuoterIn();
        req.quoterinput = new Quoterinput();
        req.quoterinput.consumer = new Consumer();

        req.quoterinput.agent_id = "1938";
        req.quoterinput.key = "afasdfasdfasdfasdfasd";
        req.quoterinput.auto_assign="true";
        req.quoterinput.consumer.brand_id = "21264";
        req.quoterinput.consumer.full_name = "Fred Smith";
        req.quoterinput.consumer.addresses_attributes[0].zip = "53704";
        .................(more code like this, but this last line is where the program fails)

Since we aren't going to take in multiple email addresses or phone numbers, I'd be fine with not having it be an array, but I'm worried that the incoming JSON with all the grouping and bracketing will fail if it's not set up like an array. I've tried to initialize instance zero like this -

    req.quoterinput.consumer.addresses_attributes[0] = new Addresses_Attributes();

.... and Visual Studio likes the code syntax-wise, but it doesn't initialize an instance when it runs, and then I get the "Object reference not set to an instance of an object." error when it runs. It hasn't allowed me to initialize the array using more generic methods since it has to map to this specific property we've already declared.

I have no doubt it's probably straightforward, but I'm not all that experienced in manipulating arrays and lists like this, and I haven't been able to find examples of arrays of sub-classes within subclasses of properties.

Just instantiate / assign the address at the same time. Something like this should work:

req.quoterinput.consumer.addresses_attributes = new []
{
      new Addresses_Attributes
      {
           zip = "53704"
      }
 };

Another way (since the property toy are trying to set is an Array), is to create a dynamic sized list, add to it, then convert it back during declaration.

var addresses = new List<Addresses_Attributes>
{
     new Addresses_Attributes {zip = "53704"}
};

req.quoterinput.consumer.addresses_attributes = addresses.ToArray();

Or, you can assign it an array of size 1, and then set it up from there:

    req.quoterinput.consumer.addresses_attributes = new Addresses_Attributes[1];
    req.quoterinput.consumer.addresses_attributes[0] = new Addresses_Attributes { zip = "666" };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM