-1

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.

bkupfer
  • 124
  • 11
Azher Aleem
  • 738
  • 6
  • 15
  • Unless you have a constructor, `new ServiceReference1.OrderInformation_type()` does not automatically create a ItemInformation array (or list). – H H May 26 '18 at 22:43
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Camilo Terevinto May 26 '18 at 22:53
  • The rest two are being initialized, the header and promotioninformation but the iteminformation isn’t. The classes are generated from wsdl automatically. How can jt miss the constructor? I think there is issue with the attribute being an array. Since the element can exist multiple times as shown in the wsdl – Azher Aleem May 26 '18 at 23:05
  • Did you find the solution to this? – Shane Ray May 29 '18 at 01:30

1 Answers1

1

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...

Shane Ray
  • 1,449
  • 1
  • 13
  • 18
  • ServiceReference1.ItemInformation = new ServiceRefernce1.ItemInformation[]; – Azher Aleem May 26 '18 at 23:23
  • Yes it does give the same not set to an instance of an object error – Azher Aleem May 26 '18 at 23:24
  • That line should be prior to the line that’s throwing the exception. Also ensure that the new exception is on the same line. That exception makes no sense on that line if you have initialized the array. – Shane Ray May 26 '18 at 23:28
  • No, that line should have a number in it : `new ServiceRefernce1.ItemInformation[10];` – H H May 27 '18 at 06:37