locked
Populating object problem RRS feed

  • Question

  • User-1458727574 posted

    I have a class that was generated from an XSD. The XML is very simple:

    <Bills>
      <Bill>
        <Fileno/>
        <FileSuffix/>
        <Status/>
        <ErrorId/>
        <ErrorMessage/>
      </Bill>
    </Bills>

    There could be many Bill objects within Bills. The Bills class has only one property which is a List<> of Bill objects. The class is here:

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.42000
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    // 
    // This source code was auto-generated by xsd, Version=4.6.1055.0.
    // 
    namespace ACLWebApi.ResponseClasses.BillingResponse
    {
        using System.Collections.Generic;
        using System.Xml.Serialization;
    
    
        /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public partial class Bills
        {
    
            private List<BillsBill> billField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("Bill")]
            public List<BillsBill> Bill
            {
                get
                {
                    return this.billField;
                }
                set
                {
                    this.billField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class BillsBill
        {
    
            private string filenoField;
    
            private string fileSuffixField;
    
            private string statusField;
    
            private int errorIdField;
    
            private string errorMessageField;
    
            /// <remarks/>
            public string Fileno
            {
                get
                {
                    return this.filenoField;
                }
                set
                {
                    this.filenoField = value;
                }
            }
    
            /// <remarks/>
            public string FileSuffix
            {
                get
                {
                    return this.fileSuffixField;
                }
                set
                {
                    this.fileSuffixField = value;
                }
            }
    
            /// <remarks/>
            public string Status
            {
                get
                {
                    return this.statusField;
                }
                set
                {
                    this.statusField = value;
                }
            }
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(DataType = "integer")]
            public int ErrorId
            {
                get
                {
                    return this.errorIdField;
                }
                set
                {
                    this.errorIdField = value;
                }
            }
    
            /// <remarks/>
            public string ErrorMessage
            {
                get
                {
                    return this.errorMessageField;
                }
                set
                {
                    this.errorMessageField = value;
                }
            }
        }
    }
    

    As I'm looping through the input data I build up a Bill each time.

    billsBill.Fileno = billingData.fileNo.ToString();
    billsBill.FileSuffix = billingData.suffix;
    billsBill.Status = "error";
    billsBill.ErrorId = statusNumber;
    billsBill.ErrorMessage = sErrorString;
    

    When I add the Bill to the Bills list it bombs out returning an Internal Server Error to the calling client. The error message is "

    NullReferenceException: Object reference not set to an instance of an object."

    The line of code that it bombs out on is:

    bills.Bill.Add(billsBill);

    Basically I want to add the Bill to the Bills list. Ultimately I have a function that serialises the Bills object to a string and returns it to the client but it doesn't allow me to add the Bill to the Bill list within Bills. What have I missed?

    Friday, September 28, 2018 9:16 AM

Answers

  • User-1458727574 posted

    Well, I fixed this. Firstly I changed the ErrorId data type so it was a string as I need a string in the response. The fault was occurring because although I had created the list in the Bills class, I hadn't initialised it to an instance of that type, so in the generated BillingResponseClass I added a constructor:

            public Bills()
            {
                this.Bill = new List<BillsBill>();
            }

    Then when I add the bill to the list, it is initialised to take it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 28, 2018 12:08 PM