-1

I have below code.

                     var cfg = _cfgUtil.GetCCfg(addr.Cnt, li.txDte);
                        if (cfg != null && cfg.Isabc)
                        {
                           await <some operation>
                        }

it is failing in second line

if (cfg != null && cfg.Isabc)

what Im not getting is , how to bypass this NUll reference check

exact error Im getting is :

System.NullReferenceException: 'Object reference not set to an instance of an object.'

user2315104
  • 2,378
  • 7
  • 35
  • 54
  • 1
    Why do you think that is the line where the Exception is thrown? There are sometimes misleading reasons about lines of code like source not matching the executing code (due to changes or building optimized output). If it really is being thrown here it could also be that `Isabc` is more than a simple getter and that this is throwing the NRE. – Igor Aug 03 '22 at 15:22
  • 1
    I'm confused - are you getting that error _with_ the null reference check? If so then the error is coming from somewhere else. – D Stanley Aug 03 '22 at 15:22
  • The answer in the [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/q/4660142/1260204) is pretty much a "how to troubleshoot a NRE", a guidebook if you will. Use the advice in that answer to figure out what is causing the NRE in your code – Igor Aug 03 '22 at 15:24
  • yes , it worked , actually , I made the correct change but forgot to save and recompile . thanks everyone – user2315104 Aug 03 '22 at 16:09

1 Answers1

0

I would say your Isabc is what has the null value.

That would need checking also, cfg.Isabc != null

Alternatively, if you are using a newish version of C# the folowing would work, without needing separate checks.

// Option A:
if(cfg != null && cfg.Isabc != null)
{
    //...
}

// Option B:
if(cfg?.Isabc is not null)
{
    //...
}

This all assumes Isabc is of type bool?, since normally a bool wouldn't be null and other types wouldn't work that way in the if statement.