NullReferenceException when TreeListView source is changed.

4 posts, 0 answers
  1. Shayne
    Shayne avatar
    5 posts
    Member since:
    May 2012

    Posted 01 Jun 2012 Link to this post

    Hello,

    I have a tree list view that contains a set of hierarchical data.  I have set up a filter to recreate the observable collection to filter out certain items from the tree.  The initial load works fine and the data is presented.  I can recreate the filter just fine.  As soon as the property changed event fires, I get a null reference exception from the tree list view.  I've tried replacing the data source collection with a new collection and I've tried just removing and adding filtered items back to the collection.  Both result in the exception below.

    I Don't have a null item in my dataset - though there are some NULLs in the data itself which is fine when the tree is first loaded.

    00000e42  mov         dword ptr [ebp+FFFFF734h],eax  

    System.NullReferenceException was unhandled
      Message=Object reference not set to an instance of an object.
      Source=Telerik.Windows.Controls.GridView
      StackTrace:
           at Telerik.Windows.Controls.TreeListView.TreeListViewRow.UpdateIsExpandable() in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\TreeListView\TreeListViewRow.cs:line 307
           at Telerik.Windows.Controls.TreeListView.TreeListViewRow.OnItemsChanged(Object sender, NotifyCollectionChangedEventArgs e) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\TreeListView\TreeListViewRow.cs:line 301
           at Telerik.Windows.Controls.TreeListView.TreeListViewRow.OnItemsCollectionChanged(HierarchicalChildCollectionView oldValue, HierarchicalChildCollectionView newValue) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\TreeListView\TreeListViewRow.cs:line 240
           at Telerik.Windows.Controls.TreeListView.TreeListViewRow.set_Items(HierarchicalChildCollectionView value) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\TreeListView\TreeListViewRow.cs:line 220
           at Telerik.Windows.Controls.RadTreeListView.InitializeHierarchyProperties(GridViewRowItem gridViewRow, Object item) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\TreeListView\RadTreeListView.cs:line 260
           at Telerik.Windows.Controls.GridView.GridViewDataControl.PrepareContainerForItemOverride(DependencyObject element, Object item) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\GridViewDataControl.cs:line 7600
           at Telerik.Windows.Controls.RadTreeListView.PrepareContainerForItemOverride(DependencyObject element, Object item) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\TreeListView\RadTreeListView.cs:line 248
           at Telerik.Windows.Controls.GridView.BaseItemsControl.Telerik.Windows.Controls.GridView.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\ItemsControl\BaseItemsControl.cs:line 336
           at Telerik.Windows.Controls.GridView.GridViewItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\ItemsControl\GridViewItemContainerGenerator.cs:line 237
           at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Virtualization\GridViewVirtualizingPanel.cs:line 2312
           at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.InsertNewContainer(Int32 childIndex, UIElement container) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Virtualization\GridViewVirtualizingPanel.cs:line 2206
           at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Virtualization\GridViewVirtualizingPanel.cs:line 2366
           at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.GenerateNextChild(IItemContainerGenerator generator, Int32 childIndex) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Virtualization\GridViewVirtualizingPanel.cs:line 1530
           at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.MeasureOverride(Size constraint) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Virtualization\GridViewVirtualizingPanel.cs:line 1228
           at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
           at System.Windows.UIElement.Measure(Size availableSize)
           at System.Windows.ContextLayoutManager.UpdateLayout()
           at System.Windows.UIElement.UpdateLayout()
           at System.Windows.Controls.TabItem.OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
           at System.Windows.UIElement.OnPreviewGotKeyboardFocusThunk(Object sender, KeyboardFocusChangedEventArgs e)
           at System.Windows.Input.KeyboardFocusChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
           at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
           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.RaiseTrustedEvent(RoutedEventArgs args)
           at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
           at System.Windows.Input.InputManager.ProcessStagingArea()
           at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
           at System.Windows.Input.KeyboardDevice.TryChangeFocus(DependencyObject newFocus, IKeyboardInputProvider keyboardInputProvider, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
           at System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
           at System.Windows.Input.KeyboardDevice.Focus(IInputElement element)
           at System.Windows.UIElement.Focus()
           at System.Windows.Controls.TabItem.SetFocus()
           at System.Windows.Controls.TabItem.OnMouseLeftButtonDown(MouseButtonEventArgs e)
           at System.Windows.UIElement.OnMouseLeftButtonDownThunk(Object sender, MouseButtonEventArgs e)
           at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
           at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
           at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
           at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
           at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
           at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
           at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
           at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
           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.RaiseTrustedEvent(RoutedEventArgs args)
           at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
           at System.Windows.Input.InputManager.ProcessStagingArea()
           at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
           at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
           at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
           at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at System.Windows.Interop.HwndSource.InputFilterMessage(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 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
           at System.Windows.Threading.Dispatcher.InvokeImpl(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.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
           at System.Windows.Application.RunDispatcher(Object ignore)
           at System.Windows.Application.RunInternal(Window window)
           at System.Windows.Application.Run(Window window)
           at System.Windows.Application.Run()
           at APPLICATION.App.Main() in APPLICATION_PATH\App.g.cs:line 0
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

  2. Shayne
    Shayne avatar
    5 posts
    Member since:
    May 2012

    Posted 01 Jun 2012 Link to this post

    Just to append - If I clear the collection or assign a new empty collection to the bound data source, the error does not occur,
  3. UI for WPF is Visual Studio 2017 Ready
  4. Shayne
    Shayne avatar
    5 posts
    Member since:
    May 2012

    Posted 01 Jun 2012 Link to this post

    Further update:

    I had to add and bind to an additional item in my data source collection.  Each item must implement an IsExpandable property.

    This eliminates the null reference exception and bind to it.  Now there is a new problem...
    What I want is to have the filtered list return to its state - so anything that is expanded should be expanded and anything that is not expanded, should not be...
                                <telerik:RadTreeListView.RowStyle>
                                    <Style TargetType="telerik:TreeListViewRow">
                                         <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
                                        <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}"/>
                                        <Setter Property="IsExpandable" Value="{Binding Path=IsExpandable, Mode=TwoWay}" />
                                    </Style>
                                </telerik:RadTreeListView.RowStyle>

    My hierarchy shows up, but if I have the IsExpanded set to two way binding, the list does not show up.  If switch the IsExpanded to a OneWay binding, my filtered list appears with the items that I am expecting.

    But then, none of the nodes are expanded - which is what I want.  I've stepped through it, and the only property that is being set is the IsExpanded - but the value is not changing, so its not being raised as a property change - If I change it to raise a property change even if the value remains unchanged (something I would not recommend), still no luck.

    Am I missing something that I should provide?
  5. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 04 Jun 2012 Link to this post

    Hi,

    You could use a RowStyleSelector for the RadTreeListView:

    public class CustomStyleSelector : StyleSelector
      {
          public override Style SelectStyle(object item, DependencyObject container)
          {
              if(container is TreeListViewRow)
              {
      
                  if(item is Item)
                  {
                      Style style = new Style() { TargetType = typeof(TreeListViewRow) };
      
                      style.Setters.Add(new Setter(TreeListViewRow.IsExpandedProperty, ((Item)item).IsExpanded));
      
                      return style;
                  }
              }
              return base.SelectStyle(item, container);
          }
      }

    Then set the change for the IsExpanded property when the RowIsExpandedChanged event is raised:

    private void RadTreeListView_RowIsExpandedChanged(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
    {
        if (((Telerik.Windows.Controls.TreeListView.TreeListViewRow)(e.Row)).IsExpanded == false)
        {
            MessageBox.Show("You just collapsed row number: "+ (this.tlv_1.Items.IndexOf(e.Row.Item) + 1).ToString());
            ((Item)e.Row.Item).IsExpanded = false;
        }
        else
        {
            ((Item)e.Row.Item).IsExpanded = true;
        }
    }

     I hope this helps.

    All the best,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top