Search code examples
c#winformsweb-servicessoapwsdl

System.NullReferenceException when using WSDL in C# SOAP Request


Another Similar But cant figure out Problem Tried the answer which worked for previous question in this same post, Which was for Arrays. Over here it is lookups and iv tried the below but its still not working. Iv also tried

 createCustomerRequest.CustomerDetails.Title[0].Code[0] = cloneTitleCode[0];

All the [0] iv switched them around and took them off too and tried all the combinations still doesnt work.

 //Security settings to match server using this line: -
 Dsoapui.https.protocols=SSLv3,TLSv1.2 ...
 BasicHttpsBinding httpsBinding = new 
 BasicHttpsBinding(BasicHttpsSecurityMode.Transport);

 //Using the security settings to create the client which will let us send/recieve the requests...
            CustomerServiceClient soapClient = new CustomerServiceClient("BasicHttpsBinding_ICustomerService");

            //Set credentials for the client...
            soapClient.ClientCredentials.UserName.UserName = username;
            soapClient.ClientCredentials.UserName.Password = password;

            //Create instances of requesting and recieving data from the server for customerDetails...
            CreateCustomer createCustomer = new CreateCustomer();
            CreateCustomerRequest createCustomerRequest = new CreateCustomerRequest();
            CreateCustomerResponse createCustomerResponse = new CreateCustomerResponse();

            //Set customer details request (so whatever will be included in the request to recieve the info)...
            createCustomerRequest.UserName = username;
            createCustomerRequest.Password = password;
            createCustomerRequest.SystemToken = "2fa192c3-1215-41f9-be71-2ba6e279494e";

            //Get current time to generate unique customer number...
            string currentTime = DateTime.Now.ToLongTimeString();
            string cleanTime = Regex.Replace(currentTime, "[^A-Za-z0-9 ]", "");

            //For WSDL arrays create the array and instance then use it like below...
            createCustomerRequest.CustomerDetails.Title = new CustomerServiceWSDL.LookupItem();
            createCustomerRequest.CustomerDetails = new CreateCustomer();
            createCustomerRequest.CustomerDetails.CustomerNumber = "001" + cleanTime;
            createCustomerRequest.CustomerDetails.FirstName = cloneFirstName;
            createCustomerRequest.CustomerDetails.Surname = cloneSurName;

            createCustomerRequest.CustomerDetails.Title.Code = cloneTitleCode;

WSDL Reference for everything im playing with in this code:

         [System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="CreateCustomer", Namespace= + WEBAPI)]
[System.SerializableAttribute()]
public partial class CreateCustomer : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

    [System.NonSerializedAttribute()]
    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private Account_Status.CustomerServiceWSDL.LookupItem BranchField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private Account_Status.CustomerServiceWSDL.LookupItem BusinessSourceField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private Account_Status.CustomerServiceWSDL.ContactInformation ContactDetailsField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string CreatedByField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private System.Nullable<System.DateTime> CreationDateField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private System.Nullable<decimal> CreditLimitField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private Account_Status.CustomerServiceWSDL.Residence CurrentAddressField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string CustomerNumberField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private System.DateTime DateOfBirthField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private Account_Status.CustomerServiceWSDL.LookupItem EmploymentStatusField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string FirstNameField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private Account_Status.CustomerServiceWSDL.Gender GenderField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string IdNumberField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string InitialsField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private Account_Status.CustomerServiceWSDL.LookupItem MaritalStatusField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string NationalInsuranceNumberField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private Account_Status.CustomerServiceWSDL.LookupItem NationalityField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private bool NoMailMarketingField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private System.Nullable<int> NoOfChildrenField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private bool NoThirdPartyMarketingField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private Account_Status.CustomerServiceWSDL.LookupItem OccupationField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string PreviousNameField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private System.Nullable<System.DateTime> ReviewDateField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private Account_Status.CustomerServiceWSDL.LookupItem StatusField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string SurnameField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private Account_Status.CustomerServiceWSDL.LookupItem TitleField;

    [global::System.ComponentModel.BrowsableAttribute(false)]
    public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
        get {
            return this.extensionDataField;
        }
        set {
            this.extensionDataField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.CustomerServiceWSDL.LookupItem Branch {
        get {
            return this.BranchField;
        }
        set {
            if ((object.ReferenceEquals(this.BranchField, value) != true)) {
                this.BranchField = value;
                this.RaisePropertyChanged("Branch");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.CustomerServiceWSDL.LookupItem BusinessSource {
        get {
            return this.BusinessSourceField;
        }
        set {
            if ((object.ReferenceEquals(this.BusinessSourceField, value) != true)) {
                this.BusinessSourceField = value;
                this.RaisePropertyChanged("BusinessSource");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.CustomerServiceWSDL.ContactInformation ContactDetails {
        get {
            return this.ContactDetailsField;
        }
        set {
            if ((object.ReferenceEquals(this.ContactDetailsField, value) != true)) {
                this.ContactDetailsField = value;
                this.RaisePropertyChanged("ContactDetails");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string CreatedBy {
        get {
            return this.CreatedByField;
        }
        set {
            if ((object.ReferenceEquals(this.CreatedByField, value) != true)) {
                this.CreatedByField = value;
                this.RaisePropertyChanged("CreatedBy");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public System.Nullable<System.DateTime> CreationDate {
        get {
            return this.CreationDateField;
        }
        set {
            if ((this.CreationDateField.Equals(value) != true)) {
                this.CreationDateField = value;
                this.RaisePropertyChanged("CreationDate");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public System.Nullable<decimal> CreditLimit {
        get {
            return this.CreditLimitField;
        }
        set {
            if ((this.CreditLimitField.Equals(value) != true)) {
                this.CreditLimitField = value;
                this.RaisePropertyChanged("CreditLimit");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.CustomerServiceWSDL.Residence CurrentAddress {
        get {
            return this.CurrentAddressField;
        }
        set {
            if ((object.ReferenceEquals(this.CurrentAddressField, value) != true)) {
                this.CurrentAddressField = value;
                this.RaisePropertyChanged("CurrentAddress");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string CustomerNumber {
        get {
            return this.CustomerNumberField;
        }
        set {
            if ((object.ReferenceEquals(this.CustomerNumberField, value) != true)) {
                this.CustomerNumberField = value;
                this.RaisePropertyChanged("CustomerNumber");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public System.DateTime DateOfBirth {
        get {
            return this.DateOfBirthField;
        }
        set {
            if ((this.DateOfBirthField.Equals(value) != true)) {
                this.DateOfBirthField = value;
                this.RaisePropertyChanged("DateOfBirth");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.CustomerServiceWSDL.LookupItem EmploymentStatus {
        get {
            return this.EmploymentStatusField;
        }
        set {
            if ((object.ReferenceEquals(this.EmploymentStatusField, value) != true)) {
                this.EmploymentStatusField = value;
                this.RaisePropertyChanged("EmploymentStatus");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string FirstName {
        get {
            return this.FirstNameField;
        }
        set {
            if ((object.ReferenceEquals(this.FirstNameField, value) != true)) {
                this.FirstNameField = value;
                this.RaisePropertyChanged("FirstName");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string Surname
    {
        get
        {
            return this.SurnameField;
        }
        set
        {
            if ((object.ReferenceEquals(this.SurnameField, value) != true))
            {
                this.SurnameField = value;
                this.RaisePropertyChanged("Surname");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.CustomerServiceWSDL.Gender Gender {
        get {
            return this.GenderField;
        }
        set {
            if ((this.GenderField.Equals(value) != true)) {
                this.GenderField = value;
                this.RaisePropertyChanged("Gender");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string IdNumber {
        get {
            return this.IdNumberField;
        }
        set {
            if ((object.ReferenceEquals(this.IdNumberField, value) != true)) {
                this.IdNumberField = value;
                this.RaisePropertyChanged("IdNumber");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string Initials {
        get {
            return this.InitialsField;
        }
        set {
            if ((object.ReferenceEquals(this.InitialsField, value) != true)) {
                this.InitialsField = value;
                this.RaisePropertyChanged("Initials");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.CustomerServiceWSDL.LookupItem MaritalStatus {
        get {
            return this.MaritalStatusField;
        }
        set {
            if ((object.ReferenceEquals(this.MaritalStatusField, value) != true)) {
                this.MaritalStatusField = value;
                this.RaisePropertyChanged("MaritalStatus");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string NationalInsuranceNumber {
        get {
            return this.NationalInsuranceNumberField;
        }
        set {
            if ((object.ReferenceEquals(this.NationalInsuranceNumberField, value) != true)) {
                this.NationalInsuranceNumberField = value;
                this.RaisePropertyChanged("NationalInsuranceNumber");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.CustomerServiceWSDL.LookupItem Nationality {
        get {
            return this.NationalityField;
        }
        set {
            if ((object.ReferenceEquals(this.NationalityField, value) != true)) {
                this.NationalityField = value;
                this.RaisePropertyChanged("Nationality");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public bool NoMailMarketing {
        get {
            return this.NoMailMarketingField;
        }
        set {
            if ((this.NoMailMarketingField.Equals(value) != true)) {
                this.NoMailMarketingField = value;
                this.RaisePropertyChanged("NoMailMarketing");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public System.Nullable<int> NoOfChildren {
        get {
            return this.NoOfChildrenField;
        }
        set {
            if ((this.NoOfChildrenField.Equals(value) != true)) {
                this.NoOfChildrenField = value;
                this.RaisePropertyChanged("NoOfChildren");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public bool NoThirdPartyMarketing {
        get {
            return this.NoThirdPartyMarketingField;
        }
        set {
            if ((this.NoThirdPartyMarketingField.Equals(value) != true)) {
                this.NoThirdPartyMarketingField = value;
                this.RaisePropertyChanged("NoThirdPartyMarketing");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.CustomerServiceWSDL.LookupItem Occupation {
        get {
            return this.OccupationField;
        }
        set {
            if ((object.ReferenceEquals(this.OccupationField, value) != true)) {
                this.OccupationField = value;
                this.RaisePropertyChanged("Occupation");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string PreviousName {
        get {
            return this.PreviousNameField;
        }
        set {
            if ((object.ReferenceEquals(this.PreviousNameField, value) != true)) {
                this.PreviousNameField = value;
                this.RaisePropertyChanged("PreviousName");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public System.Nullable<System.DateTime> ReviewDate {
        get {
            return this.ReviewDateField;
        }
        set {
            if ((this.ReviewDateField.Equals(value) != true)) {
                this.ReviewDateField = value;
                this.RaisePropertyChanged("ReviewDate");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.CustomerServiceWSDL.LookupItem Status {
        get {
            return this.StatusField;
        }
        set {
            if ((object.ReferenceEquals(this.StatusField, value) != true)) {
                this.StatusField = value;
                this.RaisePropertyChanged("Status");
            }
        }
    }


    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.CustomerServiceWSDL.LookupItem Title {
        get {
            return this.TitleField;
        }
        set {
            if ((object.ReferenceEquals(this.TitleField, value) != true)) {
                this.TitleField = value;
                this.RaisePropertyChanged("Title");
            }
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}


     [System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="LookupItem", Namespace= + APIWEB)]
[System.SerializableAttribute()]
public partial class LookupItem : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

    [System.NonSerializedAttribute()]
    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string CodeField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string DisplayTextField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private bool IsActiveField;

    [global::System.ComponentModel.BrowsableAttribute(false)]
    public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
        get {
            return this.extensionDataField;
        }
        set {
            this.extensionDataField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string Code {
        get {
            return this.CodeField;
        }
        set {
            if ((object.ReferenceEquals(this.CodeField, value) != true)) {
                this.CodeField = value;
                this.RaisePropertyChanged("Code");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string DisplayText {
        get {
            return this.DisplayTextField;
        }
        set {
            if ((object.ReferenceEquals(this.DisplayTextField, value) != true)) {
                this.DisplayTextField = value;
                this.RaisePropertyChanged("DisplayText");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public bool IsActive {
        get {
            return this.IsActiveField;
        }
        set {
            if ((this.IsActiveField.Equals(value) != true)) {
                this.IsActiveField = value;
                this.RaisePropertyChanged("IsActive");
            }
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

Solution

  • That's cause purchaseOnHoldSetRequest.AdditionalDetail is an Array type (mostly, from your posted code) which you haven't initialized and trying to set an item in index 0 saying purchaseOnHoldSetRequest.AdditionalDetail[0].Description which will bound to throw NullRefException

    Per your WSDL, the said property is array of AdditionalDetails class as seen below

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Account_Status.ServiceReference2.AdditionalDetail[] AdditionalDetail {
        get {
            return this.AdditionalDetailField;
        }
        set {
            if ((object.ReferenceEquals(this.AdditionalDetailField, value) != true)) {
                this.AdditionalDetailField = value;
                this.RaisePropertyChanged("AdditionalDetail");
            }
        }
    }
    

    Thus you need to initialize it first

        purchaseOnHoldSetRequest.UserName = "username";
        purchaseOnHoldSetRequest.Password = "password";
        purchaseOnHoldSetRequest.SystemToken = "systemtoken";
        purchaseOnHoldSetRequest.AgreementReference = customerAgreementNumberComboBox.Text;
    
    purchaseOnHoldSetRequest.AdditionalDetail = new AdditionalDetail[3]; // here
    

    And then access it likewise you are doing

       purchaseOnHoldSetRequest.AdditionalDetail[0] = new AdditionalDetail();
       purchaseOnHoldSetRequest.AdditionalDetail[0].Description = "1";