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

NullReferenceException when removing items from source collection

9 Answers 288 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michelle
Top achievements
Rank 1
Michelle asked on 09 Nov 2011, 06:16 PM
To reproduce:

1. Create a RadGridView
2. Set or bind the ItemSource to an ObservableCollection
3. Define a column with a button that removes the item
4. Click the remove button at a fast rate

Can reproduce with version 2011.2.1010.1040 or 2011.3.1020.1040 of the assemblies.

Example XAML:

<UserControl x:Class="NullReference.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
 
    <Grid x:Name="LayoutRoot" Background="White">
         
        <telerik:RadGridView x:Name="MainGrid">
 
            <telerik:RadGridView.Columns>
 
                <telerik:GridViewDataColumn Header="Name" UniqueName="Name" DataMemberBinding="{Binding Name, Mode=TwoWay}"/>
 
                <telerik:GridViewDataColumn Header="Remove" UniqueName="Remove" IsReadOnly="True">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button x:Name="RemoveButton" Content="X" Click="RemoveButton_Click"/>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewDataColumn>
 
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>

Example code-behind:

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
 
namespace NullReference
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            ItemCollection = new ObservableCollection<string>();
            for (int i = 0; i < 20; i++)
            {
                ItemCollection.Add(i.ToString());
            }
 
            MainGrid.ItemsSource = ItemCollection;
        }
 
        public ObservableCollection<string> ItemCollection { get; private set; }
 
        private void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (button == null)
            {
                return;
            }
 
            button.IsEnabled = false;
 
            string itemToRemove = button.DataContext as string;
            ItemCollection.Remove(itemToRemove);
        }
    }
}


Result:

An exception is thrown:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.Windows.Controls.GridView.GridViewRow.OnMouseButtonUpInternal(Object sender, MouseButtonEventArgs e)
   at Telerik.Windows.Input.Mouse.RaiseMouseButtonEventImpl(DependencyObject element, RoutedEvent routedEvent, MouseButton button, MouseButtonState state, MouseButtonEventArgs e)
   at Telerik.Windows.Input.Mouse.OnElementMouseLeftButtonUp(Object sender, MouseButtonEventArgs e)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)


9 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 10 Nov 2011, 07:57 AM
Hi Michelle,

 This issue is already fixed and the fix will be part of our upcoming Q3 2011 release next week.

Regards,
Vlad
the Telerik team

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

0
Michael
Top achievements
Rank 1
answered on 25 Nov 2011, 10:26 AM
Hi,
i encounter the same problem with a large data amount in a gridview (actual Q3 2011 Release).
The exception occurs while asynchronous data loading and double-clicking a row.
It's just a the next method in your GridViewRow Class.
My call-stack:

System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
   bei Telerik.Windows.Controls.GridView.GridViewRow.OnMouseDoubleClick(MouseButtonEventArgs e)
   bei System.Windows.Controls.Control.HandleDoubleClick(Object sender, MouseButtonEventArgs e)
     ....

Would be nice if you check for "GridViewDataControl != null" there too.

Greetings, Michael
0
Vlad
Telerik team
answered on 25 Nov 2011, 10:32 AM
Hello Michael,

 This is already fixed! Please download our latest build for Q3 2011. 

Best wishes,
Vlad
the Telerik team

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

0
Michael
Top achievements
Rank 1
answered on 25 Nov 2011, 11:24 AM
Hi Vlad,
thanks four your reply. I'm using the Q3.  2011.3.1116 binaries.
At the moment i check out whether the latest Hotfix (2011.3.1122) fixes the bug.
Greetings, Michael
0
Maulik Patel
Top achievements
Rank 1
answered on 09 Apr 2012, 10:41 AM
Hi,

I am facing similar problem. The grid view item is being added/removed on the cell double click event (actually allows selection). The code was working well before upgrade to latest build 2012.1.215.1040. It throws internal error when item is removed/added runtime. The error is intermittent. Is it something a known issue? 

A quick response is really appreciated.

The code snippet:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows;
using System.Windows.Media.Imaging;
 
namespace Silverlight.Core.Controls
{
    public partial class ApplicationForm : RadWindow
    {
        #region Constructors
        public ApplicationForm()
        {
            InitializeComponent();
            this.AvailableItemsGrid.AddHandler(GridViewCell.CellDoubleClickEvent, new EventHandler<Telerik.Windows.RadRoutedEventArgs>(this.OnAvailableItemsCellDoubleClick), true);
            this.SelectedItemsGrid.AddHandler(GridViewCell.CellDoubleClickEvent, new EventHandler<Telerik.Windows.RadRoutedEventArgs>(this.OnSelectedItemsCellDoubleClick), true);
            this.AvailableItemsGrid.IsEnabled = false;
            this.AvailableItemsGrid.LayoutUpdated += new EventHandler(AvailableItemsGrid_LayoutUpdated);
            this.SelectedItemsGrid.IsEnabled = false;
            this.SelectedItemsGrid.LayoutUpdated += new EventHandler(SelectedItemsGrid_LayoutUpdated);
            this.Closed += new EventHandler<WindowClosedEventArgs>(ApplicationForm_Closed);
        }
 
        private void ApplicationForm_Closed(object sender, WindowClosedEventArgs e)
        {
            if(this.SelectedItemsGrid != null)
                this.SelectedItemsGrid.LayoutUpdated -= SelectedItemsGrid_LayoutUpdated;
            if(this.AvailableItemsGrid != null)
                this.AvailableItemsGrid.LayoutUpdated -= AvailableItemsGrid_LayoutUpdated;
        }
 
        private void SelectedItemsGrid_LayoutUpdated(object sender, EventArgs e)
        {
            this.SelectedItemsGrid.IsEnabled = true;
            this.SelectedItemsGrid.RemoveHandler(GridViewCell.CellDoubleClickEvent, (EventHandler<Telerik.Windows.RadRoutedEventArgs>)this.OnSelectedItemsCellDoubleClick);
            this.SelectedItemsGrid.AddHandler(GridViewCell.CellDoubleClickEvent, new EventHandler<Telerik.Windows.RadRoutedEventArgs>(this.OnSelectedItemsCellDoubleClick), true);
        }
 
        private void AvailableItemsGrid_LayoutUpdated(object sender, EventArgs e)
        {
            this.AvailableItemsGrid.IsEnabled = true;
            this.AvailableItemsGrid.RemoveHandler(GridViewCell.CellDoubleClickEvent, (EventHandler<Telerik.Windows.RadRoutedEventArgs>)this.OnAvailableItemsCellDoubleClick);
            this.AvailableItemsGrid.AddHandler(GridViewCell.CellDoubleClickEvent, new EventHandler<Telerik.Windows.RadRoutedEventArgs>(this.OnAvailableItemsCellDoubleClick), true);
        }
        #endregion
 
        #region Private Methods
        private void OnAvailableItemsCellDoubleClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            e.Handled = true;
            System.Diagnostics.Debug.WriteLine("ApplicationForm.OnAvailableItemsCellDoubleClick:Before BtnAdd_Click");
            this.BtnAdd_Click(this, null);
            System.Diagnostics.Debug.WriteLine("ApplicationForm.OnAvailableItemsCellDoubleClick:Exit");
        }
 
        private void OnSelectedItemsCellDoubleClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            e.Handled = true;
            this.BtnRemove_Click(this, null);
        }
 
        private void BtnRemove_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnRemove_Click.Enter");
            if (GridAddedItems.SelectedItems.Count > 0)
            {
                var selectedItems = GridAddedItems.SelectedItems;
                var sourceAvailableItems = (ObservableCollection<ApplicationFormItem>)GridAvailableItems.ItemsSource;
                foreach (var item in selectedItems)
                {
                    sourceAvailableItems.Add(item as ApplicationFormItem);
                }
                var sourceAddedItems = (ObservableCollection<ApplicationFormItem>)GridAddedItems.ItemsSource;
                 
                int iCount = selectedItems.Count;
                for (int i = iCount - 1; i >= 0; i--)
                    sourceAddedItems.Remove(selectedItems[i] as ApplicationFormItem);
            }
            if (GridAddedItems.Items.Count == 0)
                BtnRemove.IsEnabled = false;
            if (GridAvailableItems.Items.Count > 0)
                BtnAdd.IsEnabled = true;
            btnOK.IsEnabled = true;
            this.AvailableItemsGrid.IsEnabled = false;
            this.SelectedItemsGrid.IsEnabled = false;
 
            System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnRemove_Click.Exit");
        }
 
        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click.Enter");
 
            if (GridAvailableItems.SelectedItems.Count > 0)
            {
                System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click.SelectedItems.Count > 0");
                var selectedItems = GridAvailableItems.SelectedItems;
                System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click.GridAvailableItems.SelectedItems assigned");
                var sourceAddedItems = (ObservableCollection<ApplicationFormItem>)GridAddedItems.ItemsSource;
                System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click.GridAddedItems.ItemsSource assigned");
                foreach (var item in selectedItems)
                {
                    System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click iterate selected items");
                    sourceAddedItems.Add(item as ApplicationFormItem);
                }
 
                var sourceAvailableItems = (ObservableCollection<ApplicationFormItem>)GridAvailableItems.ItemsSource;
                System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click.GridAvailableItems.ItemsSource assigned.");
 
                int iCount = selectedItems.Count;
                for (int i = iCount - 1; i >= 0; i--)
                {
                    System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click.Remove Selected item.");
                    sourceAvailableItems.Remove(selectedItems[i] as ApplicationFormItem);
                    System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click.Selected item removed.");
                }
            }
 
            if (GridAvailableItems.Items.Count == 0)
            {
                System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click.Disable BtnAdd.");
                BtnAdd.IsEnabled = false;
            }
            if (GridAddedItems.Items.Count > 0)
            {
                System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click.Enable BtnRemove.");
                BtnRemove.IsEnabled = true;
            }
            System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click.Enable btnOK.");
            btnOK.IsEnabled = true;
            this.AvailableItemsGrid.IsEnabled = false;
            this.SelectedItemsGrid.IsEnabled = false;
 
            System.Diagnostics.Debug.WriteLine("ApplicationForm.BtnAdd_Click.Exit.");
        }
 
        private void SetTitle()
        {
            StackPanel panel = new StackPanel() { Orientation = Orientation.Horizontal };
            Image icon = new Image() { Stretch = Stretch.Uniform };
            icon.Source = new BitmapImage(new Uri("/Silverlight.Core;component/Images/A_v01_18.png", UriKind.Relative));
            panel.Children.Add(icon);
            TextBlock title = new TextBlock();
            title.Text = Convert.ToString(this.FormTitle);
            panel.Children.Add(title);
            this.Header = panel;
        }
 
        #endregion
 
        #region Public Methods
 
        public void ShowForm()
        {
            if (this.SelectedItemsGrid.ItemsSource != null)
            {
                int cnt = Convert.ToInt32(this.SelectedItemsGrid.ItemsSource.GetType().GetProperty("Count").GetValue(this.SelectedItemsGrid.ItemsSource, null));
                if (cnt > 0)
                    BtnRemove.IsEnabled = true;
                else
                    BtnRemove.IsEnabled = false;
            }
            else
                BtnRemove.IsEnabled = false;
            if (this.AvailableItemsGrid.ItemsSource != null)
            {
                int cnt = Convert.ToInt32(this.AvailableItemsGrid.ItemsSource.GetType().GetProperty("Count").GetValue(this.AvailableItemsGrid.ItemsSource, null));
                if (cnt > 0)
                    BtnAdd.IsEnabled = true;
                else
                    BtnAdd.IsEnabled = false;
            }
            else
                BtnAdd.IsEnabled = false;
            SetTitle();
            this.ShowDialog();
        }
 
        #endregion
    }
}


Error:

Object reference not set to an instance of an object.

   at Telerik.Windows.Controls.GridView.GridViewRow.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Regards,
Maulik
0
Vlad
Telerik team
answered on 09 Apr 2012, 11:55 AM
Hi,

This is already fixed. Please get our official service pack (Q1 2012 SP1)!

All the best,
Vlad
the Telerik team

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

0
Maulik Patel
Top achievements
Rank 1
answered on 09 Apr 2012, 12:27 PM
Thanks for the quick reply. We will check after upgrading to the mentioned one.

Regards,
Maulik
0
Maulik Patel
Top achievements
Rank 1
answered on 08 May 2012, 01:16 PM
Hi  Vlad,

We are still facing the same problem. The version of GridView.dll is 2012.1.326.1040. Is it the expected version or we need to get any other version?

Regards,
Maulik
0
Yordanka
Telerik team
answered on 11 May 2012, 07:43 AM
Hi Maulik,

Thank you for the details.

We reproduced the exception using Silverlight4 binaries and it was resolved immediately. You can find it fixed with the next internal build expected on Monday.
 
Greetings,
Yordanka
the Telerik team

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

Tags
GridView
Asked by
Michelle
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Michael
Top achievements
Rank 1
Maulik Patel
Top achievements
Rank 1
Yordanka
Telerik team
Share this question
or