4

I'm getting a null reference in the PresentationFramework on my LifeShaping filtering:

enter image description here

The stack trace isn't giving me much clue:

   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& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& 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& 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)

That last line is the dialog call that shows the UX that holds the checkbox bound to ShowOnGraph.

I'm setting the live-shaping like this, based off a boolean property "ShowOnGraph":

        KPIBarsView = new CollectionViewSource { Source = KPIBars }.View;
        KPIBarsView.Filter = FilterBars;

        //grouping
        if (KPIBarsView != null && 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;
        }

Items are filtered as I'd expect when ShowOnGraphis set to false. However, as soon as I try and unfilter anything with ShowOnGraph=true I get this exception.

This is not a duplicate of "What's a null reference exception". 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).

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:

enter image description here

There's nothing null.

I'll just make a button to set a ShowOnGraph=false, and see where the exception occurs there.

Edit: Yep, it occurs "nowhere". Just opens up on a blank "Break mode" page with no content or indication of where the error occurred.

Joe
  • 6,773
  • 2
  • 47
  • 81
  • 1
    This is not a duplicate of "What's a null reference"... I know what a null reference exception is. – Joe May 11 '16 at 15:15
  • 1
    Well, have you tried debugging it and looking in your locals window to see what might be null that shouldn't be null? Visual Studio is a very powerful debugging tool (assuming you're using VS). Learn how to make use of it. – rory.ap May 11 '16 at 15:16
  • 1
    I can't debug System.Windows.Data, where the error is occurring. The debugger provides no information on what's null or not. – Joe May 11 '16 at 15:21
  • Well, then I don't know what you expect to get by posting your question here. We don't have any more insight into how `System.Windows.Data` classes work than you do. Have you tried submitting this as a bug to Microsoft? Did you do any research into this before you posted it here to see if anyone else has had this issue? – rory.ap May 11 '16 at 15:24
  • 2
    I'd like to see if I'm not missing something obvious, or if anyone else has had similar issues or knows any case where this is expected to happen before jumping right to submitting to Microsoft. Most of the time I'm at fault, rather than it being a bug in the system. I've done a fair bit of google searching and not found anything too similar. Isn't this a programming question and answer site? Either way, this is not a duplicate. Closest I could find is here: http://stackoverflow.com/questions/1553465/why-does-observablecollection-throws-an-exception-when-being-modified but that's not filtering – Joe May 11 '16 at 15:29
  • You can vote to have the question re-opened. If it gets enough votes (if you can convince enough people), it will be re-opened, and then someone can possibly provide an answer. – rory.ap May 11 '16 at 15:33
  • Will you remove your duplicate "flag" at least? – Joe May 11 '16 at 15:34
  • I've voted to re-open. That's all I can do. I don't control the banner that displays that it was voted to be closed as a duplicate. The other user who voted to close it has a gold c# badge, so he can close questions without any other close votes. – rory.ap May 11 '16 at 15:35
  • First thing to do is to capture that exception, call ToString on it (or use Copy exception details to clipboard on the exception helper dialog) and paste the entire thing into an [edit]. Edit names if you wish. Better to have the whole thing than an edited down version of it. –  May 11 '16 at 15:44
  • I suspect there is a corner case the KPIBarsViewLiveShaping maker didn't account for. You might just have to open a bug report with them. –  May 11 '16 at 15:45
  • It does look like a bug. There's not much more to the exception than everything I've put in Will, It's probably one of the most scant exceptions I've come across, but I'll post the details more explicitly. – Joe May 11 '16 at 16:00

3 Answers3

2

johnDisplayClass's comment is very helpful.

Something that worked for me: was if i also kept a member reference to each new CollectionViewSource as well as its CollectionView. This kept my live shaping and filtering working. Just this alone solved the same null ref that the OP was experiencing.

Another way to prevent this null exception is to set IsLiveSorting / IsLiveGrouping / IsLiveFiltering to false before the CollectionViewSource or CollectionView been garbage collected.

YantingChen
  • 768
  • 1
  • 12
  • 15
1

Found a solution!

I was creating the view directly (rather than using the default view, because I in had two views driven from this collection:

KPIBarsView = new CollectionViewSource { Source = KPIBars }.View;

Which I did after reading this: http://social.technet.microsoft.com/wiki/contents/articles/26673.wpf-collectionview-tips.aspx

and the following SA questions:

However, I tried just creating a new collection, populating it with the same items and using:

KPIBarsView = CollectionViewSource.GetDefaultView(KPIBars);

solved the problem. Hope this is helpful for anyone else who stumbles upon it.

Community
  • 1
  • 1
Joe
  • 6,773
  • 2
  • 47
  • 81
  • I have the [same problem](http://stackoverflow.com/questions/37394151), but I cannot use GetDefaultView because 1) I am modifying the base collection all the time and need my filters to refresh and 2) I'm using Live Shaping which is not available with GetDefaultView. Do you have a suggestion? – marczellm May 23 '16 at 14:58
  • 1
    I see you had the "This is a duplicate!" problem too. – Joe May 23 '16 at 15:51
  • Does it happen when you remove an object from the collection you are filtering (so not just filtering it out, physically removing the object)? – Joe May 23 '16 at 15:53
  • It's a very weird scenario. To reproduce it I have to 1) edit an unrelated property of the same object in another control and 2) drag and drop the object between two databound ItemsControls where a drop event handler also modifies the object. – marczellm May 23 '16 at 21:31
  • I reduced it to a "minimal" example and edited that into my original question, – marczellm May 24 '16 at 09:34
  • Might be worth putting it in a new question so it gets some visibility (and not mentioning null reference exception in the title!) – Joe May 24 '16 at 10:02
  • 7
    Something that worked for me: was if i also kept a member reference to each new CollectionViewSource as well as its CollectionView. This kept my live shaping and filtering working. Just this alone solved the same null ref that the OP was experiencing. – johnDisplayClass May 30 '17 at 20:09
  • @johnDisplayClass: Could you add that as an answer? This is a very important observation which should be documented here on StackOverflow. Comments can be removed without notice, so this should be in an answer instead. – Heinzi Nov 02 '18 at 14:06
0

What I recommend is that you set yourself up to debug the Microsoft dll's from within your solution

https://msdn.microsoft.com/en-us/library/cc667410.aspx

Then make sure your debug settings have all of the possible exception types checked, then when you run your app again and break on the exception, you'll get a full stack trace that could help you work out the isse.

Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
  • Hmm, It's loaded symbols for WindowBase.dll and PresentationFramework.dll but still shows "No source available" for this particular exception. I'll keep trying to get some debug info. – Joe May 11 '16 at 17:30