title: How to resolve Value cannot be null. Parameter name: source in linq?
body: <p>I don't know why I get this kind of error. It happens sometimes, and I suspicious of my code that still have thread running while I close my Application. So when I open again it happens.</p>
  <pre><code>Value cannot be null.
  Parameter name: source
  StackTree :
     at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)
     at Susenas2015.ViewModels.Kuesioner.VMVsen15_KVal.SettingValidationAndRange(List`1 listTextBox, List`1 listCheckBox, TabControl tabControl) in d:\handita\Office\Project\Susenas 2015\Aplikasi Template Survei\Susenas2015\ViewModels\Kuesioner\VMVsen15_KVal.cs:line 430
     at Susenas2015.ViewModels.Kuesioner.VMVsen15_KVal.vSen15_K_Loaded(Object sender, RoutedEventArgs e) in d:\handita\Office\Project\Susenas 2015\Aplikasi Template Survei\Susenas2015\ViewModels\Kuesioner\VMVsen15_KVal.cs:line 846
     at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
     at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
     at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
     at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
     at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
     at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
     at MS.Internal.LoadedOrUnloadedOperation.DoWork()
     at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
     at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
     at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
     at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
     at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
     at System.Windows.Interop.HwndTarget.OnResize()
     at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wpa`
  </code></pre>
  <p>My code here it is</p>
  <pre><code>    private void SettingValidationAndRange(List&lt;TextBox&gt; listTextBox, List&lt;CheckBox&gt; listCheckBox, TabControl tabControl)
          {
  
              List&lt;string&gt; listNotDeclare = new List&lt;string&gt;();
  
              foreach (var textB in listTextBox)
              {
                  if (textB.Tag != null)
                      break;
  
                  Metadata metadata = ListMetadataKor.Where(
                      x =&gt; &quot;text&quot; + x.Field == textB.Name // this line 430
  
                  ).FirstOrDefault();
  
                  if (metadata == null)
                  {
                      if (!string.IsNullOrEmpty(textB.Name))
                          listNotDeclare.Add(textB.Name);
                  }
                  else
                  {
                      metadata.TabControl = tabControl;
                      textB.Tag = metadata;
                  }
  
                  textB.AddEvents();
                  textB.AutomateFocus();
  
              }
  
              if (listNotDeclare.Count &gt; 0)
              {
                  Clipboard.SetText(string.Join(&quot;,&quot;, listNotDeclare.ToArray()));
                  Dialog.Info(&quot;Ada beberapa Metadata tidak ditemukan data sudah dicopy ke clipboard&quot;);
              }
  
          }
  </code></pre>
  <p>When I start my application for my first time, it doesn't get any error. It happens when I open in 2nd or more. And if I open my application it would stuck in that code.</p>
  <p>How I can solve this? I'm pretty sure that my Property <code>ListMetadataKor</code> is not <code>null</code></p>
  <p>And ListMetadataKor is instance of <code>List&lt;Metadata&gt;</code> object that I have created. It happens only in rare cases. And I don't know to solve it</p>
  <h2>UPDATE</h2>
  <p>This is my code in image<img src="https://i.stack.imgur.com/a9MKP.jpg" alt="enter image description here" /></p>
  <p>I fill <code>ListMetadataKor</code> with this code</p>
  <pre><code>BWHelper.Run((s, e) =&gt;
  {
      DataTable dataMetaDataKOR = ExcelHelper.GetDataTableFromExcel(
          AppConstants.FILE_METADATA, AppConstants.SHEET_METADATA_KOR
      );
  
      DataTable dataKonsistensiKOR = ExcelHelper.GetDataTableFromExcel(
           AppConstants.FILE_METADATA, AppConstants.SHEET_KONSISTENSI_KOR
       );
  
      listKonsistensiKor = Tools.ToolConvert.GetKonsistensi(dataKonsistensiKOR);
      listMetadataKor = Tools.ToolConvert.GetMetadata(dataMetaDataKOR);
  
      foreach (Metadata metadata in listMetadataKor)
      {
          metadata.Range.ProsesRange();
      }
  
  }, (s, e) =&gt;
  {
      try
      {
          kor = new VSEN15_K() { Title = &quot;Validasi Susenas - KOR&quot; };
          kor.DataContext = new VMVsen15_KVal(rtSusenas.MasterRT, kor, this, listKonsistensiKor, listMetadataKor);
          kor.PreviewKeyDown += EventsCollection.EnterAsTabPreviewKeyDown;
          vmHome.HideLoading();
          UpdateMetaDataEntriKOR(RTSusenas.MasterRT);
          kor.ShowDialog();
      }
      catch (Exception Ex)
      {
          vmHome.HideLoading();
          Dialog.Error(Ex);
      }
  });
  </code></pre>
  <p>And then I throw the variable through constructor of my class</p>
  <pre><code> public VMVsen15_KVal(
          MasterRT masterRT,
          VSEN15_K vSen15_K,
          IDaftarSusenas vmDaftarRTSusenas,
          List&lt;Konsistensi&gt; listKonsistensiKor,
          List&lt;Metadata&gt; listMetadataKor
  
          )
      {
  
          ListArtDetail = new ObservableCollection&lt;ARTDetailVal&gt;();
  
          this.ListKonsistensiKor = listKonsistensiKor;
          this.ListMetadataKor = listMetadataKor;
  </code></pre>
  <p>My tools konsistensi like this</p>
  <pre><code>public static List&lt;Konsistensi&gt; GetKonsistensi(DataTable dataTable)
  {
      List&lt;Konsistensi&gt; listMetadata = new List&lt;Konsistensi&gt;();
      for (int i = 0; i &lt; dataTable.Rows.Count; i++)
      {
          Konsistensi k = new Konsistensi();
          object[] required = new object[] { DBNull.Value, &quot;&quot; };
          k.Field = dataTable.Rows[i][FIELD].ToString();
          if (string.IsNullOrWhiteSpace(k.Field)) continue;
          k.Message = dataTable.Rows[i][MESSAGE].ToString();
          var obj = dataTable.Rows[i][ORDER];
          k.Order = !required.Contains(dataTable.Rows[i][ORDER]) ? Convert.ToInt32(dataTable.Rows[i][ORDER]) : (int?)null;
          k.Page = !required.Contains(dataTable.Rows[i][PAGE]) ? Convert.ToInt32(dataTable.Rows[i][PAGE]) : (int?)null;
          k.Perlakuan = dataTable.Rows[i][PERLAKUAN].ToString();
          k.RelFields = dataTable.Rows[i][RELFIELDS].ToString();
          k.Rule = dataTable.Rows[i][RULE].ToString();
  
          if (dataTable.Rows[i][LEVEL].ToString().ToUpper() == (&quot;ART&quot;))
              k.LevelKonsistensi = LevelKonsistensi.ART;
          else if (dataTable.Rows[i][LEVEL].ToString().ToUpper() == (&quot;RT&quot;))
              k.LevelKonsistensi = LevelKonsistensi.RT;
          else if (dataTable.Rows[i][LEVEL].ToString().ToUpper() == (&quot;RTWARNING&quot;))
              k.LevelKonsistensi = LevelKonsistensi.RTWarning;
          else if (dataTable.Rows[i][LEVEL].ToString().ToUpper().Contains(&quot;ARTWARNING&quot;))
              k.LevelKonsistensi = LevelKonsistensi.ARTWarning;
          else
              k.LevelKonsistensi = LevelKonsistensi.Lain;
  
          //k.LevelKonsistensi = dataTable.Rows[i][LEVEL].ToString().Contains(&quot;ART&quot;) ? LevelKonsistensi.ART : LevelKonsistensi.RT;
          if (k.IsEmpty())
              continue;
  
          listMetadata.Add(k);
      }
      return listMetadata;
  }
  </code></pre>