开发者

Need to check for null on something that throws exception

_sponsorOrBankFacingBorrowerCompanyWizardData = CompanyData.GetCompanyWizardData(SponsorOrBankFacingBorrowerCompany.CompanyID);

So the problem here is that the code tries to go into this s开发者_如何学编程tatement no matter what, I want it to only go if it CAN go, obviously.

However, if I perform a watch in Visual Studio 2010 ONLY on this object SponsorOrBankFacingBorrowerCompany, no properties, methods, anything, I get a null reference exception.

How do I check for null when I get an exception before the check even happens?

Here's the watch:

Need to check for null on something that throws exception

EDIT:

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);
            }
        }

Getter for other that is throwing exception:

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


Something like the following, unless I'm mistaking your question:

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

Update:

Okay, this is elaborate but will hopefully make the problem become immediately evident, so let's drill this down, the long way; can you amend your property code to look like the following:

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;
        }
    }
}


I'm betting you're getting a null reference from your SponsorOrBankFacingBorrowerCompany getter in here:

STPData.AffiliateTradeIndicator.Value

Check all those parts in the debugger...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜