title: Linq Where clause eventually results in NullReferenceException
body: <p>I have a website hosted in IIS. I am caching values in a repository to save numerous trips to the database. The cache is instantiated at the top of the repo like so:</p>
  <pre><code>protected static List&lt;MaxDeposit&gt; _cached_values = new List&lt;MaxDeposit&gt;();
  </code></pre>
  <p>When a call is made to retrieve data from the db, I'm checking the cache first to see if we already have it:</p>
  <pre><code>protected List&lt;MaxDeposit&gt; GetMaxDepositsFromCacheByTier(int tierId)
  {
      return _cached_values.Where(x =&gt; x.TierId == tierId).ToList();
  }
  </code></pre>
  <p>This works as expected and either returns a value or values, or an empty list. If an empty list is returned, the data is retrieved from the db and then added to the cache like so:</p>
  <pre><code>protected void AddMaxDepositsToCache(List&lt;MaxDeposit&gt; maxDeposits)
  {
       if (maxDeposits == null) return;
  
       lock (this)
       {
          _cached_values.AddRange(maxDeposits);
       }
   }
  </code></pre>
  <p>This is in production and seems to be working fine for a few days until we start seeing NullReferenceExceptions on the Return statement:</p>
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
     at MyProject.Model.Data.Repositories.MaxDepositRepository.&lt;&gt;c__DisplayClass3_0.&lt;GetMaxDepositsFromCacheByTier&gt;b__0(MaxDeposit x) in Data\Repositories\MaxDepositRepository.cs:line 31
     at System.Linq.Enumerable.WhereListIterator&lt;T&gt;.MoveNext()
     at System.Collections.Generic.List&lt;T&gt;..ctor(IEnumerable&lt;T&gt; collection)
     at System.Linq.Enumerable.ToList[TSource](IEnumerable&lt;T&gt; source)
     at MyProject.Model.Data.Repositories.MaxDepositRepository.GetMaxDepositsFromCacheByTier(Int32 tierId) in Data\Repositories\MaxDepositRepository.cs:line 31
     at MyProject.Model.Data.Repositories.MaxDepositRepository.GetByTier(Int32 tierId) in Data\Repositories\MaxDepositRepository.cs:line 0
  </code></pre>
  <p>Once this happens, every call to this code then throws the same exception. Recycling the application pool resolves the problem and calls are made successfully again.</p>
  <p>I assumed that somehow _cached_values ended up becoming NULL (even though at no point in code is it set to NULL), so I modified the code as such:</p>
  <pre><code>protected List&lt;MaxDeposit&gt; GetMaxDepositsFromCacheByTier(int tierId)
          {
              if (_cached_values == null)
              {
                  _cached_values = new List&lt;MaxDeposit&gt;();
  
                  _logger.LogInformation($&quot;MaxDepositRepository.GetMaxDepositsFromCacheByTier(int tierId): _cached_values field was null. tierId = {tierId}&quot;);
              }
  
              return _cached_values.Where(x =&gt; x.TierId == tierId).ToList();
          }
  </code></pre>
  <p>So now I null check _cached_values before working with it, and instantiate a new empty list if it is NULL, and log that we've had to do so. This change is in production, but the same issue happened again yesterday. The added logging was not found, which tells me _cached_values was not NULL at any point.</p>
  <p>Is the problem to do with the Linq Where clause? If so, why does it work for a while before breaking?</p>
  <p>Thanks in advance if someone is able to explain what's happening here or pinpoint where I'm going wrong!</p>