Search code examples
c#asp.netweb-serviceswsdlnullreferenceexception

WSDL element is null?


I have an WSDL document and it has elements like this above.

  <s:element name="NewPortalOrder">
    <s:complexType>
      <s:sequence>
        <s:element minOccurs="1" maxOccurs="1" name="OrderType" type="tns:CardSalesType" />
        <s:element minOccurs="1" maxOccurs="1" name="Customer" type="tns:PortalCustomerContainer" />
        <s:element minOccurs="1" maxOccurs="1" name="InvoiceAddress" type="tns:AddressContainer" />
        <s:element minOccurs="1" maxOccurs="1" name="DeliveryAddress" type="tns:AddressContainer" />
        <s:element minOccurs="0" maxOccurs="1" name="Cards" type="tns:ArrayOfPortalCardContainer" />
      </s:sequence>
    </s:complexType>
  </s:element>

I added the WSDL file as a ServiceReference into the solution.

On the server side

  WS.NewPortalOrderRequest order = new WS.NewPortalOrderRequest();

  order.InvoiceAddress.AddressLine1 = txtAddress.Text;
  order.InvoiceAddress.AddressLine2 = txtAddress2.Text;

On the order.InvoiceAddress.AddressLine1 = txtAddress.Text; line I get the Object reference not set to an instance of an object. error.

When I watch the order.InvoiceAddress, I see that is null. Why can I get this error? How can I solve it?


Solution

  • Most likely you need to create an instance of InvoiceAddress in the NewPortalOrderRequest, like this:

    WS.NewPortalOrderRequest order = new WS.NewPortalOrderRequest();
    
    order.InvoiceAddress = new WS.InvoiceAddress();
    
    // Now you can assign values to InvoiceAddress' property:
    order.InvoiceAddress.AddressLine1 = txtAddress.Text;
    order.InvoiceAddress.AddressLine2 = txtAddress2.Text;
    

    You might need to do order.InvoiceAddress = new WS.AddressContainer() based on the WSDL, but I'm not 100% sure on that one.