通过WSDL将值分配到自定义类型数组时出现空引用错误

时间:2011-03-09 10:50:10

标签: .net arrays web-services nullreferenceexception

我有一个WSDL:http://kinkom.dk/WCFAPP/SI_CreateSM_OB_7.wsdl(我已经更改了地址位置)。

我的代码是通过WSDL从SAP访问数据;将其绑定到.NET控件以及通过相同的WSDL将数据从.NET控件发送到SAP。

代码如下:

// instantiating web service method, web service name is : Webrefence_SM_New2
Webrefence_SM_New2.DT_CreateSM_CR ReqDT = new Webrefence_SM_New2.DT_CreateSM_CR(); 
Webrefence_SM_New2.DT_SM_Response RespDT;

// instantiate cookie to hold the session
CookieContainer cookie = new CookieContainer();

// Make a proxy of webservice
Webrefence_SM_New2.SI_CreateSM_OBService _proxy1 = new Webrefence_SM_New2.SI_CreateSM_OBService();
_proxy1.Credentials = new NetworkCredential("xxx", "xxx"); // use credential to acess to the SAP system
_proxy1.CookieContainer = cookie;

// binding user input
ReqDT.B_Xn_Type = DDLBusinessTrnscType.SelectedValue;
ReqDT.BP_Function1 = "";
ReqDT.BP_Function2 = "";
ReqDT.BP_Function3 = "";
ReqDT.BP_Function4 = "";

// Error come here when I am assiging a string value
ReqDT.ResourceType[0].Val = "ab";

//The error is: Null Reference Exception  {"Object reference not set to an instance of an object."}
// getting output from WSDL 
RespDT = _proxy1.SI_CreateSM_OB(ReqDT);

// Presenting the output to GUI 
Lblmessageresponse.Text = Convert.ToString(RespDT.Status);

另一方面,当我从.net控件在load事件中绑定数据时,没有问题。

代码如下:

Webrefence_SM_New2.DT_SM_InputHelp_Request IncomingtypeReq = new Webrefence_SM_New2.DT_SM_InputHelp_Request();
Webrefence_SM_New2.DT_SM_InputHelp IncomingTypeResp;

// instantiate cookie to hold the session
CookieContainer cookie = new CookieContainer();

// Make a proxy of webservice
Webrefence_SM_New2.SI_CreateSM_OBService _proxy1 = new Webrefence_SM_New2.SI_CreateSM_OBService();
_proxy1.Credentials = new NetworkCredential("xxx", "xxx"); // use credential to acess to the SAP system
_proxy1.CookieContainer = cookie;
IncomingtypeReq.OptionalText = "op";
IncomingTypeResp = _proxy1.SI_GetInputHelp(IncomingtypeReq);

// Bind value to Drop down list
DDLStatus.DataSource = IncomingTypeResp.Status;
DDLStatus.DataTextField = "val";
DDLStatus.DataValueField = "val";
DDLStatus.DataBind();

如果有人能帮我解决如何解决空引用错误,那将会很棒。

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

ReqDT.ResourceType = new DT_Value[10];
ReqDT.ResourceType[0] = new DT_Value();
ReqDT.ResourceType[0].val = "ab";