This is a migrated thread and some comments may be shown as answers.

NullReferenceException in Item_Prepared

5 Answers 112 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Gabriele Eigel
Top achievements
Rank 1
Gabriele Eigel asked on 09 Mar 2010, 10:20 AM
Hi,

I try to expand items by path in a RadTreeView and sometimes occurs a NullReferenceException. The stack trace is:
  

  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.RadTreeView.RadTreeView_ItemPrepared(object sender = {Telerik.Windows.Controls.RadTreeView}, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e = {Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs}) + 0x10c bytes 

Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.RadTreeView.OnItemPrepared(Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e = {Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs}) + 0x42 bytes 
  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.RadTreeView.PrepareContainerForDescendant(System.Windows.DependencyObject element = {Schleswig-Holstein}, object item = {Schleswig-Holstein}, Telerik.Windows.Controls.ItemsControl owner = {Germany}) + 0x244 bytes 
  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.RadTreeViewItem.PrepareContainerForItemOverride(System.Windows.DependencyObject element = {Schleswig-Holstein}, object item = {Schleswig-Holstein}) + 0x60 bytes 
  System.Windows.dll!System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(System.Windows.DependencyObject container = {Schleswig-Holstein}, object item = {Schleswig-Holstein}) + 0x56 bytes 
  System.Windows.dll!System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(System.Windows.DependencyObject container = {Schleswig-Holstein}) + 0x69 bytes 
  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.TreeView.TreeViewPanel.InsertContainer(int childIndex = 0, System.Windows.UIElement container = {Schleswig-Holstein}, bool isRecycled = false) + 0x21f bytes 
  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.TreeView.TreeViewPanel.InsertNewContainer(int childIndex = 0, System.Windows.UIElement container = {Schleswig-Holstein}) + 0x2b bytes 
  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.TreeView.TreeViewPanel.AddContainerFromGenerator(int childIndex = 0, System.Windows.UIElement child = {Schleswig-Holstein}, bool newlyRealized = true) + 0xcf bytes 
  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.TreeView.TreeViewPanel.MeasureOverride(System.Windows.Size constraint = {Infinity,Infinity}) + 0xec1 bytes 
  System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget = 171078864, float inWidth = Infinity, float inHeight = Infinity, out float outWidth = 0.0, out float outHeight = 0.0) + 0x13d bytes 
  [Native to Managed Transition] 
  [Managed to Native Transition] 
  System.Windows.dll!MS.Internal.XcpImports.FrameworkElement_MeasureOverride(System.Windows.FrameworkElement element = {Germany}, System.Windows.Size availableSize = {Infinity,Infinity}) + 0x8e bytes 
  System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.Windows.Size availableSize = {Infinity,Infinity}) + 0x42 bytes 
  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.RadTreeViewItem.MeasureOverride(System.Windows.Size availableSize = {Infinity,Infinity}) + 0x3b9 bytes 
  System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget = 161547216, float inWidth = Infinity, float inHeight = Infinity, out float outWidth = 0.0, out float outHeight = 0.0) + 0x13d bytes 
  [Native to Managed Transition] 
  [Managed to Native Transition] 
  System.Windows.dll!MS.Internal.XcpImports.UIElement_Measure(System.Windows.UIElement element = {Germany}, System.Windows.Size availableSize = {Infinity,Infinity}) + 0x68 bytes 
  System.Windows.dll!System.Windows.UIElement.Measure(System.Windows.Size availableSize = {Infinity,Infinity}) + 0xc1 bytes 
  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.TreeView.TreeViewPanel.MeasureOverride(System.Windows.Size constraint = {Infinity,Infinity}) + 0x1002 bytes 
  System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget = 171076040, float inWidth = Infinity, float inHeight = Infinity, out float outWidth = 0.0, out float outHeight = 0.0) + 0x13d bytes 
  [Native to Managed Transition] 
  [Managed to Native Transition] 
  System.Windows.dll!MS.Internal.XcpImports.FrameworkElement_MeasureOverride(System.Windows.FrameworkElement element = {EMEA / LATAM}, System.Windows.Size availableSize = {Infinity,Infinity}) + 0x8e bytes 
  System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.Windows.Size availableSize = {Infinity,Infinity}) + 0x42 bytes 
  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.RadTreeViewItem.MeasureOverride(System.Windows.Size availableSize = {Infinity,Infinity}) + 0x3b9 bytes 
  System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget = 171266368, float inWidth = Infinity, float inHeight = Infinity, out float outWidth = 0.0, out float outHeight = 0.0) + 0x13d bytes 
  [Native to Managed Transition] 
  [Managed to Native Transition] 
  System.Windows.dll!MS.Internal.XcpImports.MethodEx(System.IntPtr ptr = 171266368, string name = "UpdateLayout", MS.Internal.CValue[] cvData = null) + 0x15b bytes 
  System.Windows.dll!MS.Internal.XcpImports.MethodEx(System.Windows.DependencyObject obj = {EMEA / LATAM}, string name = "UpdateLayout") + 0x48 bytes 
  System.Windows.dll!MS.Internal.XcpImports.UIElement_UpdateLayout(System.Windows.UIElement element = {EMEA / LATAM}) + 0x39 bytes 
  System.Windows.dll!System.Windows.UIElement.UpdateLayout() + 0x1e bytes 
  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.RadTreeView.GetItemByPath(string path = "EMEA / LATAM|Germany|Schleswig-Holstein", string separator = "|") + 0x217 bytes 
  Telerik.Windows.Controls.Navigation!Telerik.Windows.Controls.RadTreeView.ExpandItemByPath(string path = "EMEA / LATAM|Germany|Schleswig-Holstein", string separator = "|") + 0x113 bytes 

Is this bug noted? Is a bug fix planed for this?

Best regards,
   Gabriele

5 Answers, 1 is accepted

Sort by
0
Miro Miroslavov
Telerik team
answered on 10 Mar 2010, 12:15 PM
Hello Gabriele Eigel,

This isn't a known bug. Can you please give us some more information how to reproduce it. It'll be great if you send us a demo project or you can give more info like if TreeView is data bound, do you have any custom templates etc.

Greetings,
Miro Miroslavov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Gabriele Eigel
Top achievements
Rank 1
answered on 15 Mar 2010, 07:03 PM
Hi Miro,

thank you for answering. I've tried to attach a zip file with an example ( a solution), it doesn't work. Can I send this file on another way?

Greetings,
   Gabriele

Code snippet:
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Net;  
using System.Windows;  
using System.Windows.Controls;  
using System.Windows.Documents;  
using System.Windows.Input;  
using System.Windows.Media;  
using System.Windows.Media.Animation;  
using System.Windows.Shapes;  
using System.Collections.ObjectModel;  
using System.Collections;  
using System.ComponentModel;  
using Telerik.Windows.Controls;  
using System.Windows.Threading;  
using System.Diagnostics;  
 
namespace TreeViewPerformance  
{  
    public partial class MainPage : UserControl  
    {  
        IEnumerable<TreeNodeItem> data;  
        int searchedItemIndex;  
        List<TreeNodeItem> allNodes;  
        IEnumerable<TreeNodeItem> searchResult;         
 
        public MainPage()  
        {  
            InitializeComponent();  
            btnSearch.Click += new RoutedEventHandler(btnSearch_Click);  
            this.btnFindNext.Click += new RoutedEventHandler(btnFindNext_Click);  
            this.btnFindPrev.Click += new RoutedEventHandler(btnFindPrev_Click);  
            this.Pulse.Begin();  
        }  
        private void btnFindNext_Click(object sender, RoutedEventArgs e)  
        {  
            try  
            {  
                if (this.searchResult != null)  
                {  
                    if (searchedItemIndex < this.searchResult.Count() - 1)  
                    {  
                        searchedItemIndex = searchedItemIndex + 1;  
 
                        // find item, expand the path (if not already visible) and select it  
                        expandItemAndSelect(this.searchResult.ElementAt(searchedItemIndex));  
 
                        // make find next button visible (and the search button invisible) if there is a next searched entry  
                        handleSearchButtons(this.searchResult.Count() > searchedItemIndex + 1, true);  
                    }  
                }  
            }  
            catch (Exception ex)  
            {  
                Debug.WriteLine("Exception occurs in find next: " + ex.StackTrace);  
            }  
        }  
 
        private void btnFindPrev_Click(object sender, RoutedEventArgs e)  
        {  
            try  
            {  
                if (this.searchResult != null)  
                {  
                    if (searchedItemIndex > 0)  
                    {  
                        searchedItemIndex = searchedItemIndex - 1;  
 
                        // find item, expand the path (if not already visible) and select it  
                        expandItemAndSelect(this.searchResult.ElementAt(searchedItemIndex));  
 
                        // make find next button visible (and the search button invisible) if there is a next searched entry  
                        handleSearchButtons(this.searchResult.Count() > searchedItemIndex + 1, searchedItemIndex > 0);  
                    }  
                }  
            }  
            catch (Exception ex)  
            {   
                Debug.WriteLine("Exception occurs in find previous: " + ex.StackTrace);   
            }  
        }  
        void btnSearch_Click(object sender, RoutedEventArgs e)  
        {  
            try  
            {  
                // get all items with the searched text in label  
                if (txtSearch.Text != String.Empty)  
                {  
                    searchResult = this.allNodes.Where(n => n.Label.ToUpper().Contains(this.txtSearch.Text.ToUpper()));  
 
                    if (searchResult != null && searchResult.Count() > 0)  
                    {  
                        searchedItemIndex = 0;  
 
                        // find item, expand the path (if not already visible) and select it  
                        expandItemAndSelect(searchResult.First<TreeNodeItem>());  
 
                        // make find next button visible (and the search button invisible) if there is a next searched entry  
                        handleSearchButtons(searchResult.Count() > searchedItemIndex + 1, false);  
                    }  
                }  
            }  
            catch (Exception ex)  
            {  
                Debug.WriteLine("Exception occurs in search button click: " + ex.StackTrace);  
            }  
        }  
 
        private void expandItemAndSelect(TreeNodeItem item)  
        {  
            try  
            {  
                treeView.ExpandItemByPath(item.Path.Substring(0, item.Path.Length - 1), "|");  
                treeView.GetItemByPath(item.Path.Substring(0, item.Path.Length - 1), "|").IsSelected = true;  
            }  
            catch (Exception ex)  
            {  
                Debug.WriteLine("Exception occurs in expandItemAndSelect: " + ex.StackTrace);  
            }  
        }  
 
        private void handleSearchButtons(bool findNextEnabled, bool findPrev)  
        {  
            btnSearch.Visibility = !findNextEnabled ? Visibility.Visible : Visibility.Collapsed;  
            btnFindNext.Visibility = findNextEnabled ? Visibility.Visible : Visibility.Collapsed;  
            btnFindPrev.Visibility = findPrev ? Visibility.Visible : Visibility.Collapsed;  
        }  
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)  
        {  
            this.ButtonOut.Begin();  
            this.Blink.Begin();  
 
            BackgroundWorker worker = new BackgroundWorker();  
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);  
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);  
            worker.RunWorkerAsync();  
 
            treeView.ItemPrepared += new EventHandler<RadTreeViewItemPreparedEventArgs>(treeView_ItemPrepared);  
        }  
 
        void treeView_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)  
        {  
            try  
            {  
                e.PreparedItem.IsLoadOnDemandEnabled = e.PreparedItem.Level < 4;  
            }  
            catch (Exception ex)  
            {  
                Debug.WriteLine("Exception occurs in treeView_ItemPrepared: " + ex.StackTrace);  
            }  
        }  
 
        void worker_DoWork(object sender, DoWorkEventArgs e)  
        {  
            try  
            {  
                allNodes = new List<TreeNodeItem>();   
                // create some data to be displayed in tree, 5 levels  
                TreeNodeItem root = new TreeNodeItem() { Label = "Test root 1"Path = "Test root 1|" };  
 
                ObservableCollection<TreeNodeItem> items1 = new ObservableCollection<TreeNodeItem>();  
                for (int i = 0; i < 2; i++)  
                {  
                    TreeNodeItem item1 = new TreeNodeItem() { Label = "Test child 1" + i, Path = root.Path + "Test child 1" + i + "|" };  
 
                    ObservableCollection<TreeNodeItem> items2 = new ObservableCollection<TreeNodeItem>();  
                    for (int j = 0; j < 10; j++)  
                    {  
                        TreeNodeItem item2 = new TreeNodeItem() { Label = "Test child 1" + i + j, Path = item1.Path + "Test child 1" + i + j + "|" };  
 
                        ObservableCollection<TreeNodeItem> items3 = new ObservableCollection<TreeNodeItem>();  
                        for (int k = 0; k < 10; k++)  
                        {  
                            TreeNodeItem item3 = new TreeNodeItem() { Label = "Test child 1" + i + j + k, Path = item2.Path + "Test child 1" + i + j + k + "|" };  
 
                            ObservableCollection<TreeNodeItem> items4 = new ObservableCollection<TreeNodeItem>();  
                            for (int l = 0; l < 10; l++)  
                            {  
                                TreeNodeItem item4 = new TreeNodeItem() { Label = "Test child 1" + i + j + k + l, Path = item3.Path + "Test child 1" + i + j + k + l + "|" };  
                                allNodes.Add(item4);  
                                items4.Add(item4);  
                            }  
                            item3.Children = items4;  
                            allNodes.Add(item3);  
                            items3.Add(item3);  
                        }  
                        item2.Children = items3;  
                        allNodes.Add(item2);  
                        items2.Add(item2);  
                    }  
                    item1.Children = items2;  
                    allNodes.Add(item1);  
                    items1.Add(item1);  
                }  
                root.Children = items1;  
                allNodes.Add(root);  
                data = new List<TreeNodeItem>() { root };  
            }  
            catch (Exception ex)  
            {  
                Debug.WriteLine("Exception occurs in create data: " + ex.StackTrace);  
            }  
        }  
 
        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)  
        {  
            treeView.ItemsSource = data;  
 
            DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(1000) };  
            timer.Tick += (s, args) => 
            {  
                this.LoadingOut.Begin();  
                this.Blink.Stop();  
                timer.Stop();  
            };  
 
            timer.Start();  
        }  
    }      
 
    public static class ObservableExtensions  
    {  
        public static IEnumerable<T> ToObservable<T>(this IEnumerable<T> target)  
        {  
            var result = new ObservableCollection<T>();  
            foreach (var item in target.Cast<T>())  
            {  
                result.Add(item);  
            }  
            return result;  
        }  
    }  
      
    public class TreeNodeItem  
    {  
        private ObservableCollection<TreeNodeItem> children;  
        public ObservableCollection<TreeNodeItem> Children  
        {  
          get { return children; }  
          set { children = value; }  
        }  
        private String label;  
        public String Label  
        {  
          get { return label; }  
          set { label = value; }  
        }  
        private String path;  
        public String Path  
        {  
          get { return path; }  
          set { path = value; }  
        }  
 
        public TreeNodeItem()  
        {  
            children = new ObservableCollection<TreeNodeItem>();  
        }  
 
        // Needed to build path with  
        public override string ToString()  
        {  
            return this.Label;  
        }  
    }  
}  
 
0
Miroslav
Telerik team
answered on 18 Mar 2010, 03:07 PM
Hi Gabriele Eigel,

I created a project based on your code but I could not reproduce the exception (I used the 2010.Q1) assemblies, it is attached to my reply.

Can you have a look at the project and see whether you are experiencing this issue?
Which version of the controls are you using?
Is it an option to switch to the latest version if the problem is resolved there?

You say that you experience this only sometimes, is there something special when it occurs?

Sincerely yours,
Miroslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Gabriele Eigel
Top achievements
Rank 1
answered on 19 Mar 2010, 02:56 PM
Hi Miroslav,

thank you for support. I've tried to use your solution with my old dlls (2009.3.1314.1030) and then mit the new dlls (2010.1.218.1030). The error ocurrs by both, but it is only visible in VisualStudio output.

I attach 2 screenshots, on the color of the scrollbar you can see that it is made with another version of dlls.
To get the exception I enter "1" in the search field, click first on search and then 11 times on find next (the searching select first the tree node "Test child 10000" then goes down until Test child 10009, select Test child 1000 and on next find next occurs the exception).

Can you reproduce it?

Best Regards,
   Gabriele
0
Miroslav
Telerik team
answered on 25 Mar 2010, 12:50 AM
Hello Gabriele Eigel

Yes, we were able to reproduce it.

Thank you for helping us to get to this issue!

I was quite surprised to see  the exception in the output window and not surfacing at runtime. It  seems that it happens on the layout thread and it is only quietly reported.

The fix for this exception will be available with this week's internal build and the coming SP1 release.

I have attached the project with updated assemblies where the issue should not be present.

Your Telerik Points have been updated for your feedback.

Greetings,
Miroslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
Gabriele Eigel
Top achievements
Rank 1
Answers by
Miro Miroslavov
Telerik team
Gabriele Eigel
Top achievements
Rank 1
Miroslav
Telerik team
Share this question
or