0

当我尝试保存配置文件时,我得到对象引用未设置为对象错误的实例。

我尝试添加一个“if”语句 if (this.Profile.ProfileDetail != null ) 但它仍然给了我同样的错误。

这是我的代码:

private void SaveProfile()
    {


            this.Profile.ProfileDetail.FullName = txtfname.Text;
            this.Profile.ProfileDetail.IdentityCardNo = txtcardno.Text;
            this.Profile.ProfileDetail.IdentityCardColour = ddlcardcolor.SelectedValue;
            this.Profile.ProfileDetail.IdentityCardExpiryDate = DateTime.Parse(txtcardexp.Text);
            this.Profile.ProfileDetail.Race = ddlrace.SelectedValue;
            this.Profile.ProfileDetail.Religion = ddlreligion.SelectedValue;
            this.Profile.ProfileDetail.ContactInformation.Address1 = txtaddress1.Text;
            this.Profile.ProfileDetail.ContactInformation.Address2 = txtaddress2.Text;
            this.Profile.ProfileDetail.ContactInformation.Address3 = txtaddress3.Text;
            this.Profile.ProfileDetail.ContactInformation.Postcode = txtpostcode.Text;
            this.Profile.ProfileDetail.ContactInformation.District = ddldistrict.SelectedValue;
            this.Profile.ProfileDetail.ContactInformation.OfficePhone = txtphoneoffice.Text;
            this.Profile.ProfileDetail.ContactInformation.HomePhone = txtphonehome.Text;
            this.Profile.ProfileDetail.ContactInformation.MobilePhone = txtphonecell.Text;
            this.Profile.ProfileDetail.ContactInformation.Email = txtemail.Text;




        //this.Profile.ProfileDetail.FirstName = txtFirstName.Text;
        //this.Profile.ProfileDetail.LastName = txtLastName.Text;
        //this.Profile.ProfileDetail.IdentityCardNo = txtICNo.Text;
        //this.Profile.ProfileDetail.DateOfBirth = DateTime.Parse(txtDateOfBirth.Text);
        //this.Profile.ProfileDetail.Gender = (Gender)Enum.Parse(typeof(Gender), ddlGender.SelectedValue, true);
        //this.Profile.Preferences.TimeZone = ddlTimeZones.SelectedValue;
        //this.Profile.Preferences.Culture = ddlCultures.SelectedValue;

        this.Profile.Save();

        this.ResetElements();
    }
4

1 回答 1

0

该行this.Profile.ProfileDetail可能不是引发异常的行。你有没有进入代码?它可能与.SelectedValue. 如果没有选择值,它将抛出 null。

于 2013-07-17T03:20:18.107 回答