需要在抛出异常的东西上检查null

时间:2011-03-22 13:27:22

标签: c# .net nullreferenceexception

_sponsorOrBankFacingBorrowerCompanyWizardData = CompanyData.GetCompanyWizardData(SponsorOrBankFacingBorrowerCompany.CompanyID);

所以这里的问题是代码试图进入这个语句,无论如何,我希望它只有在它可以去的时候才会去。显然。

但是,如果我只在Visual Studio 2010中对此对象SponsorOrBankFacingBorrowerCompany执行监视,没有属性,方法,任何内容,我会得到一个空引用异常。

如果在检查发生之前我收到异常,如何检查null?

这是手表:

enter image description here

编辑:

public STPProject STPData
        {
            get { return _STPData; }
            set
            {
                _STPData = value;

                //set WebIds
                foreach (STPCompany comp in _STPData.STPCompanyCollection)
                {
                    comp.WebId = comp.WebId < 1 ? GetNextWebId() : comp.WebId;
                    foreach (STPContact cont in comp.STPContactCollection)
                    {
                        cont.WebId = cont.WebId < 1 ? GetNextWebId() : cont.WebId;
                    }
                }

                //must be before AttachSTPEvents
                _STPData.AffiliateTradeIndicator = _STPData.AffiliateTradeIndicator.HasValue ? _STPData.AffiliateTradeIndicator.Value : false;

                //set company wizard defaults
                _sponsorOrBankFacingBorrowerCompanyWizardData = CompanyData.GetCompanyWizardData(SponsorOrBankFacingBorrowerCompany.CompanyID);

                AttachSTPEvents(_STPData);
            }
        }

吸引异常的其他人:

public STPCompany SponsorOrBankFacingBorrowerCompany
        {
            get
            {
                if (STPData.AffiliateTradeIndicator.Value)
                {
                    return BankFacingBorrower;
                }
                else
                {
                    return Sponsor;
                }
            }
        }

2 个答案:

答案 0 :(得分:2)

如下所示,除非我误解你的问题:

if (SponsorOrBankFacingBorrowerCompany != null)
{
    _sponsorOrBankFacingBorrowerCompanyWizardData = 
        CompanyData.GetCompanyWizardData(
            SponsorOrBankFacingBorrowerCompany.CompanyID);
}

<强>更新

好的,这是详细的,但希望能让问题立即变得明显,所以让我们深入研究这个问题;你能修改你的房产代码,如下所示:

public STPCompany SponsorOrBankFacingBorrowerCompany
{
    get
    {
        if (STPData == null)
        {
            throw new InvalidOperationException("'STPData' is null");
        }

        if (STPData.AffiliateTradeIndicator == null)
        {
            throw new InvalidOperationException(
                "'STPData.AffiliateTradeIndicator' is null");
        }

        if (STPData.AffiliateTradeIndicator.Value == null)
        {
            throw new InvalidOperationException(
                "'STPData.AffiliateTradeIndicator.Value' is null");                ;
        }

        if (STPData.AffiliateTradeIndicator.Value)
        {
            return BankFacingBorrower;
        }
        else
        {
            return Sponsor;
        }
    }
}

答案 1 :(得分:0)

我打赌你在这里从SponsorOrBankFacingBorrowerCompany getter获得一个null引用:

STPData.AffiliateTradeIndicator.Value

检查调试器中的所有这些部分......