WSDL元素是null?

时间:2013-04-30 08:05:25

标签: c# asp.net web-services wsdl nullreferenceexception

我有一个WSDL文档,它有如上所示的元素。

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

我将WSDL文件作为ServiceReference添加到解决方案中。

在服务器端

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

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

order.InvoiceAddress.AddressLine1 = txtAddress.Text;行,我收到Object reference not set to an instance of an object.错误。

当我看order.InvoiceAddress时,我看到它是空的。为什么我会收到此错误?我该如何解决?

1 个答案:

答案 0 :(得分:3)

您很可能需要在InvoiceAddress中创建NewPortalOrderRequest的实例,如下所示:

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;

您可能需要根据WSDL执行order.InvoiceAddress = new WS.AddressContainer(),但我对此并不是100%肯定。