简体   繁体   中英

foreach looping variable throws NullReferenceException but Enumerable is not null

Some people decided to close my previous question, but the question they linked ( What is a NullReferenceException, and how do I fix it? ) did not have an answer. This question is fundamentally different since the enumerable is populated. It is not null. Just as the first answer stated, I placed "strategic breakpoints" and checked the variables.

I'm debugging a XUnit test and it turns out that in my business logic the iteration variable in the foreach loop is throws an exception "Object Reference not set to instance of object". However, the list over which the iteration is happening is NOT null. I can see that when I'm debugging. Here is the code:

Business logic:

List<string> regionArray = new List<string>();
if (someCondition)
{
    regionArray = _utils.GetRegions(someParam); // this is not returning null
}
foreach (var region in regionArray)
{
      var query = from dataSet in myDataSets
                  where dataSet.Location == region
                  select dataSet;
      var queryResult = query.FirstOrDefault();
      if (queryResult == null)
      {
           // do stuff
      } else if (queryResult.State != States.Provisioned)
      {
           // do stuff
      }
}

Here is how I am mocking the _utils.GetRegions call, but I dont think thats the problem.

private Mock<IUtils> _mockRegionUtils;

[Fact]
public void ItWorks()
{
    // do stuff
    _mockRegionUtils = new Mock<IUtils>();
    _mockRegionUtils.Setup(utils => utils.GetRegions(It.IsAny<ISomeParam>())).Returns(new List<string>() {"america", "china"});
    // call business logic
}

I have checked all the types in the debugger. regionArray.GetType() returns {System.Collections.Generic.List`1[System.String]} . when I type region into the console however, i get:

region
'region' threw an exception of type 'System.NullReferenceException'

how is this possible?

EDIT: fixed a typo above, sorry about that. Something weird though, so if I reassign the value of regionArray to be an inline list, it still fails. But if I define a new inline list and iterate over that, the looping works fine.

List<string> regionArray = new List<string>();
if (someCondition)
{
    regionArray = _utils.GetRegions(someParam); // this is not returning null
}
regionArray = new List<string>() {"china", "america"};
List<string> temp = new List<string>() {"foo", "bar"}
foreach (var region in regionArray)
{
      // region still throws null reference exception
      foreach (var tempVar in temp)
      {
          var c = tempVar; // this works. tempvar is never null.
      }
      var query = from dataSet in myDataSets
                  where dataSet.Location == region
                  select dataSet;
      var queryResult = query.FirstOrDefault();
      if (queryResult == null)
      {
           // do stuff
      } else if (queryResult.State != States.Provisioned)
      {
           // do stuff
      }
}

EDIT 2: So I tried iterating over the regionArray in the same way just before the logic above, and it worked fine.

List<string> regionArray = new List<string>();
if (someCondition)
{
    regionArray = _utils.GetRegions(someParam); // this is not returning null
}

foreach (var region in regionArray)
{
      var c = region; // this works
}

foreach (var region in regionArray)
{
      // region throws null reference exception
      var query = from dataSet in myDataSets
                  where dataSet.Location == region
                  select dataSet;
      var queryResult = query.FirstOrDefault();
      if (queryResult == null)
      {
           // do stuff
      } else if (queryResult.State != States.Provisioned)
      {
           // do stuff
      }
}

so most likely, it is not a problem with the moq object. based on @Iliar's suggestion, I will see if regionArray gets modified, but at first glance since regionArray is not used within the loop, my answer would be "no".

Update: I got around this issue by renaming the region looping variable to a different name. As it turns out, I was doing another foreach (var region...) loop earlier in my code. I spoke to some senior colleagues as to why these 2 names would conflict with each other, and they said maybe it was some issue with symbols in VSCode and not really with my actual code. Thank you all for your help!

There was a lot of info in this thread, so just to summarize here are a few bulletpoints in case it is helpful to someone else in the future:

  • When debugging an XUnit test, I was seeing my looping variable in my foreach displaying the following info in the tooltip 'region' threw an exception of type 'System.NullReferenceException' Data [IDictionary]:{System.Collections.ListDictionaryInternal} HResult [int]:-2147467261 HelpLink [string]:null InnerException [Exception]:null Message [string]:"Object reference not set to an instance of an object." Source [string]:"9dd66c33104045bba27ad3fc9fb95185" StackTrace [string]:" at <>x.<>m0(<IngestEvents>d__13 <>4__this)" TargetSite [MethodBase]:{System.String <>m0(<IngestEvents>d__13)} Static members... 'region' threw an exception of type 'System.NullReferenceException' Data [IDictionary]:{System.Collections.ListDictionaryInternal} HResult [int]:-2147467261 HelpLink [string]:null InnerException [Exception]:null Message [string]:"Object reference not set to an instance of an object." Source [string]:"9dd66c33104045bba27ad3fc9fb95185" StackTrace [string]:" at <>x.<>m0(<IngestEvents>d__13 <>4__this)" TargetSite [MethodBase]:{System.String <>m0(<IngestEvents>d__13)} Static members... .

  • even as I stepped INTO the loop, the tooltip for region was still showing the above, and when I typed region into the console, I got 'region' threw an exception of type 'System.NullReferenceException'.

  • The above 2 points led me to believe region was null. However, through @IVSoftware 's help, I verified that region was not actually null, because the assertion was passing.

  • I then looked at the rest of my code, and as a random guess, I tried renaming the looping variable region to something else. When I did, region was correctly set to the elements of the list.

Hi I really hope to be helpful. First I will answer your question "how is this possible?" and I think I can explain why your edited question with the inline list works. True, the GetRegions method returns a list that is not null. Sure, if you call GetType() on this it correctly identifies it as a "list of strings". I believe however, that the GetRegions method is returning a list that contains at least one null value . And you prove it out yourself when you added the edit, because you say this works:

regionArray = new List<string>() {"china", "america"};

But try making a list with three values like this where one of them is null and probably the loop will fail again.

regionArray = new List<string>() {"china", null, "america"};

This suggests a bug inside the GetRegions method is putting a null value into the list that it is returning. Adding these two lines at the beginning of your loop might go a long way to identifying the issue:

        foreach (var region in regionArray)
        {
            // Look for this in the Visual Studio 'Output' window 
            System.Diagnostics.Debug.WriteLine(region == null ? "Null" : region);
            // You believe that region is not null. So 'assert' that 
            // this evaluates to True and if it doesn't the debugger will break here.
            System.Diagnostics.Debug.Assert(region != null, "Break on this line if region is null");
    ...

From what I can tell, I would look inside your GetRegions(someParam) method and see if it's inserting a null into the list somewhere. Good luck!

List<string> regionArray = new List<string>();
if (someCondition)
{
    regionArray = _utils.GetRegions(someParam); // this is not returning null
}

this will override the regionArray instance, to the GetRegions instance, so creating new List<string> instance is useless.

What you want is:

List<string> regionArray = new List<string>();
if (someCondition)
{
    regionArray.AddRange(_utils.GetRegions(someParam)); 
}

which is using this new instance, and add the resulted elements inside it. If _utils.GetRegions(someParam) returns empty set, then regionArray will not be null, but it'll be empty regionArray.Count == 0 .

this is also can be done using ToList as well:

var regionArray = _utils.GetRegions(someParam).ToList();

now, you need to check regionArray after that:

if(regionArray.Count == 0) 
{
    // do something 
}

Or using Linq

if(!regionArray.Any())
{
    // do something 
}

if the collection is not empty then you can iterate through the list and validate each string inside this list before you process it:

foreach (var region in regionArray)
{
    // check if the element is null or empty 
    // if true, will skip this element and go to the next one
    if(string.IsNullOrEmpty(region)) { continue; } // go to the next iteration

    // get the results    
    var queryResult = myDataSets.FirstOrDefault(x=> x.Location == region);

    if (queryResult == null)
    {
       // do stuff
    } 
    else if (queryResult.State != States.Provisioned)
    {
       // do stuff
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM