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

.net 通过WSDL将值分配到自定义类型数组时出现空引用错误,.net,arrays,web-services,nullreferenceexception,.net,Arrays,Web Services,Nullreferenceexception,我有一个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();

我有一个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);
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();
另一方面,当我在from load事件中将数据与.net控件绑定时,就没有问题了

代码如下所示:

// 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);
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();
如果有人能帮我解决空引用错误,那就太好了。

尝试以下方法:

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

John的可能重复:但对我来说,我真的无法初始化我的数组,虽然数组是自定义类型,并在SAP端定义,但我知道自定义类型。数组类型为DT_值,数组名称为:ResourceType,数组内部元素为string。在.NET中放置ResourceType[0].Val=“ab”时,会出现错误。我不能像这样分配数组:ReqDT.ResourceType[]rt=new ReqDT.ResourceType[10];因此,我不能初始化它。这可能是空引用错误的原因。但我不知道如何修复它?任何帮助??使用
ReqDT.ResourceType=新的DT_值[10]。如果不是10号的话,可以使用你需要的任何尺寸。然后,对于每个
ResourceType
您需要执行类似于
requdt.ResourceType[0]=new DT_Value()的操作。谢谢。例如:如果我将like requdt.ResourceType[0]=新的DT_值(“ab”);对于传递一个固定值,你认为应该有效吗?@保罗:是的。您只需要初始化元素。否则他们就有了
null
,这给了你
NullReferenceException
。亲爱的约翰,我非常感谢你。它正在工作。如果你住在丹麦哥本哈根,我可以邀请你共进晚餐!!:)@保罗:不客气。您可以通过阅读和学习如何避免接下来的几个NullReferenceException来感谢我,否则您将看到这些异常。嗨,John,您知道如何使用SOAP附件吗?如果你有一个好的例子,请转发。@Paul:别问我,问问我。另外问一个问题。嗨,John,如果您有时间查看链接,我将不胜感激: