2017-09-05 58 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在數組的情況下有點複雜,因爲你真的需要首先用預定義的元素數創建數組。或者,您可以使用列表而不是字符串[]作爲替代。 –