简体   繁体   English

如何在属性的子类中初始化数组? (未设置对象引用……错误)

[英]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.我必须构建一个应用程序/Web 服务来接收 JSON 查询。 The JSON is generated by a widget that we can not customize. JSON 是由我们无法自定义的小部件生成的。 I used Visual Studio to create the class structure from a JSON example.我使用 Visual Studio 从 JSON 示例创建 class 结构。

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.我想测试我的服务,我在 SOAPUI 中遇到错误,所以我需要一些可以调试的东西,所以我正在硬编码 JSON 输入 object 以作为我的服务参考。

Here are the classes, as they reside on the web service -这是类,因为它们驻留在 web 服务上 -

[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.查看Consumer的子类,有几个子类是arrays——address_attributes、phone_attributes和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."我能够初始化主 JSON 输入/查询容器,然后我可以初始化主 class 和“消费者”的子类。 I can enter data for the main fields, and then fields in the sub-class of Consumer, but I can't initialize the arrays.我可以为主要字段输入数据,然后在 Consumer 的子类中输入字段,但我无法初始化 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.因为我们不打算接收多个 email 地址或电话号码,所以我可以不用它是一个数组,但我担心传入的 JSON 如果没有设置所有分组和包围将失败像一个数组一样。 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." .... 并且 Visual Studio 喜欢代码语法,但它在运行时不会初始化实例,然后我得到“对象引用未设置为 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.它不允许我使用更通用的方法初始化数组,因为它必须 map 到我们已经声明的这个特定属性。

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.我毫不怀疑这可能很简单,但是我在操作 arrays 和这样的列表方面并不是很有经验,而且我还没有找到 arrays 的子类在属性子类中的示例。

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:或者,您可以为其分配一个大小为 1 的数组,然后从那里进行设置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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