2017-09-05 44 views
0

这是我作为作者在stackoverflow上的首次亮相。我堆积...通用类的SOAP web服务引用返回nullexception

我有Windows窗体应用程序。我将ServiceReference添加到.wsdl Web服务。一切正常,但我从初始化泛型类的字段有问题。

该类看起来,下面:

public partial class setHuntingGroupRequest : object, System.ComponentModel.INotifyPropertyChanged { 

    private string fwSessionIdField; 

    private string pbxNameField; 

    private AlcHuntingGroup huntingGroupField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=0)] 
    public string fwSessionId { 
     get { 
      return this.fwSessionIdField; 
     } 
     set { 
      this.fwSessionIdField = value; 
      this.RaisePropertyChanged("fwSessionId"); 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=1)] 
    public string pbxName { 
     get { 
      return this.pbxNameField; 
     } 
     set { 
      this.pbxNameField = value; 
      this.RaisePropertyChanged("pbxName"); 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=2)] 
    public AlcHuntingGroup huntingGroup { 
     get { 
      return this.huntingGroupField; 
     } 
     set { 
      this.huntingGroupField = value; 
      this.RaisePropertyChanged("huntingGroup"); 
     } 
    } 

所以我有fwSessionId,pbxName和嵌套AlcHuntingGroup看起来:

public partial class AlcHuntingGroup : object, System.ComponentModel.INotifyPropertyChanged { 

     private string directoryNumberField; 

     private string directoryNameField; 

     private AlcHuntingGroupSearchType searchTypeField; 

     private string[] membersField; 

     private bool unavailableAuthorizedField; 

     private bool releaseAfterTimerField; 

     private string overflowDirectoryNumberField; 

     private int entityField; 

(...)

如何我的代码工作:

 private void btnDodajnrdohg_Click(object sender, EventArgs e) 
     { 

     SoapDemo.ServiceReference1.AlcPbxManagementPortTypeClient soap = new 
     SoapDemo.ServiceReference1.AlcPbxManagementPortTypeClient(); 

     setHuntingGroupRequest request = new setHuntingGroupRequest(); 
     setHuntingGroupResponse response = new setHuntingGroupResponse(); 

     request.fwSessionId = session_id; 
     request.pbxName = "demooxemai42"; 

     try 
      { 

      response = soap.setHuntingGroup(request); 

      this.output.Text = response.resultCode.ToString(); 
      } 
      catch (Exception error) 
      { 

      this.output.Text = "Error in request: " + error + "\n"; 
      } 
      } 

在上面的示例中,我插入以请求两个字段。不幸的是,我还必须插入AlcHuntingGroup类的所有字段(huntingGroup是对这个类的引用)。我尝试这样做:

request.fwSessionId = session_id; 
request.pbxName = "demooxemain2"; 
request.huntingGroup.directoryName = "Directory Name"; 
request.huntingGroup.directoryNumber = "1001"; 
request.huntingGroup.entity = 1; 
//etc 

智能感知正确地看到这一领域,但是当我开始调试这个代码是返回我一个错误的线在那里我有request.huntingGroup.directoryName = "Directory Name";

System.NullReferenceException:对象引用未设置到对象的实例 SoapDemo.ServiceReference1.setHuntingGroupRequest.huntingGroup.get返回null。

如何防止从huntingGroup获得空值?

回答

0
request.huntingGroup.directoryName = "Directory Name"; 
您的代码

这部分

request.huntingGroup 

调用的属性获取。您需要通过这样的第一初始化或改进的getter:

get { 
     return this.huntingGroupField ?? (this.huntingGroupField = new AlcHuntingGroup()); 
    } 

这种方法称为“懒惰初始化” - huntingGroupField会当您尝试财产第一次访问huntingGroup被自动初始化。

+0

感谢Man ...你是最棒的。我用你的getter编辑huntingGroup,现在它工作正常。 –

+0

我该如何设置字符串数组的getter? 我有这样的领域: '[System.Xml.Serialization.XmlElementAttribute( “成员”,令= 3)] 公共字符串[] {成员获取 { 回报this.membersField; } set { this.membersField = value; this.RaisePropertyChanged(“members”); } }' 当我插入请求时,f.e. 'request.huntingGroup.members [0] =“2003”'它返回我完全相同的错误。 @antonnorko –

+0

@MateuszSzafraniec在数组的情况下有点复杂,因为你真的需要首先用预定义的元素数创建数组。或者,您可以使用列表而不是字符串[]作为替代。 –