title: "Object reference not set to an instance of an object" in PresentationFramework with Live Shaping
body: <p>I'm getting a null reference in the PresentationFramework on my LifeShaping filtering:</p>
  
  <p><a href="https://i.stack.imgur.com/Sy7w5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Sy7w5.png" alt="enter image description here"></a></p>
  
  <p>The stack trace isn't giving me much clue:</p>
  
  <pre><code>   at System.Windows.Data.ListCollectionView.RestoreLiveShaping()
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
     at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
     at System.Windows.Threading.DispatcherOperation.InvokeImpl()
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Windows.Threading.DispatcherOperation.Invoke()
     at System.Windows.Threading.Dispatcher.ProcessQueue()
     at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)
     at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)
     at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
     at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
     at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
     at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
     at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&amp; msg)
     at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
     at System.Windows.Window.ShowHelper(Object booleanBox)
     at System.Windows.Window.ShowDialog()
     at MVVMSeaCores.AppWindowManager.ShowDialog(Object rootModel, Object context, IDictionary`2 settings)
  </code></pre>
  
  <p>That last line is the dialog call that shows the UX that holds the checkbox bound to <code>ShowOnGraph</code>.</p>
  
  <p>I'm setting the live-shaping like this, based off a boolean property "ShowOnGraph":</p>
  
  <pre><code>        KPIBarsView = new CollectionViewSource { Source = KPIBars }.View;
          KPIBarsView.Filter = FilterBars;
  
          //grouping
          if (KPIBarsView != null &amp;&amp; KPIBarsView.CanGroup == true)
          {
              KPIBarsView.GroupDescriptions.Clear();
              KPIBarsView.GroupDescriptions.Add(new PropertyGroupDescription("KPIViewModel.ContextViewModel"));
          }
  
          //Live Filtering
          ICollectionViewLiveShaping KPIBarsViewLiveShaping = KPIBarsView as ICollectionViewLiveShaping;
          if (KPIBarsViewLiveShaping.CanChangeLiveFiltering)
          {
              KPIBarsViewLiveShaping.LiveFilteringProperties.Add("ShowOnGraph");
              KPIBarsViewLiveShaping.IsLiveFiltering = true;
          }
  </code></pre>
  
  <p>Items are filtered as I'd expect when <code>ShowOnGraph</code>is set to false. However, as soon as I try and unfilter anything with <code>ShowOnGraph=true</code> I get this exception.</p>
  
  <p><strong>This is not a duplicate of <a href="https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it">"What's a null reference exception"</a>.</strong> I know what a null reference exception is. But in this case, the null reference is in the Presentation Framework, in System.Windows.Data. I've got no idea what's null, why (the list doesn't contain any null entries, the filter Property is a bool and cannot be null).</p>
  
  <p>The null object isn't in my code, and isn't available for me to debug. All I get in the debugger is where in the dispatch it was when this occured. In one case, it's in the dialog that contains the list where I'm setting it to true:</p>
  
  <p><a href="https://i.stack.imgur.com/wmJBL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wmJBL.png" alt="enter image description here"></a></p>
  
  <p>There's nothing null.</p>
  
  <p>I'll just make a button to set a ShowOnGraph=false, and see where the exception occurs there.</p>
  
  <p>Edit: Yep, it occurs "nowhere". Just opens up on a blank "Break mode" page with no content or indication of where the error occurred.</p>