Search code examples
c#.netwsdl

Object reference not set to an instance of an object while initializing items for a Soap request as per the WSDL


I have a WSDL which contains OrderInformation_type class consisting of three private attributes which are classes themselves: Header_type, PromotionInformation_type and an array of ItemInformation_type[] respectively. I have initialized an object of OrderInformation_type as 'order'. Further, I have initialized order.Header_type header and order.PromotionInformation_type objects which are initialized successfully and their attributes can be set easily. But when I try to initialize the order.ItemInformation_type[] object I get an error at run time stating, object reference not set to an instance of an object.Considering, that the OrderInformation_type has ItemInformation_type attribute as an array thus I initialize it in the following way:

WindowsFormsApplication1.ServiceReference1.OrderInformation_type orderinfo = new ServiceReference1.OrderInformation_type();
            orderinfo.Header = new ServiceReference1.Header_type();
            orderinfo.Header.AccountNumber = 496570;
            orderinfo.Header.DistributorIdentifier = ServiceReference1.Header_typeDistributorIdentifier.MBA;

            orderinfo.PromotionInformation = new ServiceReference1.PromotionInformation_type();
            **orderinfo.ItemInformation[0] = new ServiceReference1.ItemInformation_type();**
            orderinfo.ItemInformation[0].ItemID = "95847";
            orderinfo.ItemInformation[0].ItemIDType = ServiceReference1.ItemInformation_typeItemIDType.D;
            orderinfo.ItemInformation[0].Quantity = 1;

The Bold one is the line where it get the error.


Solution

  • orderinfo.ItemInformation is an array. You are trying to assign to [0] on something that has not been created yet. Adding

    orderinfo.ItemInformation = new new ServiceReference1.PromotionInformation_type[];

    should do it...