InvalidOperationException: Failed to compare two elements in the array.

9 posts, 0 answers
  1. Jonathon
    Jonathon avatar
    50 posts
    Member since:
    Aug 2011

    Posted 06 Dec 2011 Link to this post

    Hi,

    I decided to make a new thread for this issue:
    My GridView is a collection of IActivity which is an interface I made that 3 classes implement. This only happens when I try to adda new activity to the grid when it is sorted. When the Grid is not sorted, the activity is added to the grid. This is the error:

    {System.InvalidOperationException: Failed to compare two elements in the array. ---> System.InvalidCastException: Unable to cast object of type 'FirstActivityViewModel' to type 'SecondActivityViewModel'.
       at Telerik.Windows.Data.FuncExtensions.<>c__DisplayClass1`2.<ToUntypedFunc>b__0(Object item)
       at Telerik.Windows.Data.FunctionComparer.Compare(Object x, Object y)
       at System.Collections.Generic.ArraySortHelper`1.InternalBinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
       at System.Collections.Generic.ArraySortHelper`1.BinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
       --- End of inner exception stack trace ---
       at System.Collections.Generic.ArraySortHelper`1.BinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
       at System.Array.BinarySearch[T](T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
       at System.Collections.Generic.List`1.BinarySearch(Int32 index, Int32 count, T item, IComparer`1 comparer)
       at System.Collections.Generic.List`1.BinarySearch(T item, IComparer`1 comparer)
       at Telerik.Windows.Data.KeyedCollection.IndexOf(Object value)
       at Telerik.Windows.Data.QueryableCollectionView.InternalIndexOf(Object item)
       at Telerik.Windows.Data.QueryableCollectionView.IndexOf(Object item)
       at Telerik.Windows.Data.DataItemCollection.IndexOf(Object value)
       at Telerik.Windows.Controls.DataControl.ItemCoerce(DependencyObject d, Object baseValue)
       at Telerik.Windows.PropertyMetadata.DoCoercion(Func`3 areValuesEqual, DependencyObject d, DependencyProperty dp, CoerceValueCallback coerceCallback, Object defaultBaseValue, Boolean forceNewBaseValue, Boolean& isReentrantImplicitCoercion)
       at Telerik.Windows.PropertyMetadata.PropertyChangeHook.OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
       at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
       at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
       at System.Windows.Data.BindingExpression.SendDataToTarget()
       at System.Windows.Data.BindingExpression.SourcePropertyChanged(PropertyPathListener sender, PropertyPathChangedEventArgs args)
       at System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(PropertyPathStep source)
       at System.Windows.PropertyAccessPathStep.RaisePropertyPathStepChanged(PropertyListener source)
       at System.Windows.CLRPropertyListener.SourcePropertyChanged(Object sender, PropertyChangedEventArgs args)
       at System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(Object sender, PropertyChangedEventArgs args)
       at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
       at GalaSoft.MvvmLight.ViewModelBase.RaisePropertyChanged(String propertyName)
       at Telerik.Windows.Controls.RadSplitButton.ExecuteCommand()
       at Telerik.Windows.Controls.RadSplitButton.OnClick()
       at Telerik.Windows.Controls.RadSplitButton.ActionButton_Click(Object sender, RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at Telerik.Windows.Controls.RadButton.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.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)}

    Is there a fix for this?

    Thanks,

    Jonathon
  2. Christian Burkhardt
    Christian Burkhardt avatar
    23 posts
    Member since:
    Feb 2010

    Posted 07 Dec 2011 Link to this post

    I have exactly the same error. It wasn't there in the previous version of the gridview.
  3. Christian Burkhardt
    Christian Burkhardt avatar
    23 posts
    Member since:
    Feb 2010

    Posted 07 Dec 2011 Link to this post

    I now compiled the sources and debugged it. The problem is in the FuncExtensions class, specifically this method:

            public static Func<object, object> ToUntypedFunc<T, TResult>(Func<T, TResult> func)
            {
                return item => func.Invoke((T) item);
            }

    It tries to cast to T, but if you have items of multiple types in the gridview, it will be called once for the "wrong" type of object and try to cast it to T and fail.

    Looking forward to a fix, as this actually broke my app :(
  4. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 07 Dec 2011 Link to this post

    Hi,

     Will post more info about your scenario? What is your current grid version and what was the previous version?

    Greetings,
    Vlad
    the Telerik team

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

  5. Jonathon
    Jonathon avatar
    50 posts
    Member since:
    Aug 2011

    Posted 08 Dec 2011 Link to this post

    I'm currently using 2011.3.1116.1040. 
  6. Luc
    Luc avatar
    14 posts
    Member since:
    Dec 2007

    Posted 17 Jan 2012 Link to this post

    Same error using  2011.3 1220 for SL5.. Any fix to this sortdescriptor issue
  7. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 17 Jan 2012 Link to this post

    Hi,

     Please open support ticket and send us an example project where we can reproduce and debug the exception. 

    Kind regards,
    Vlad
    the Telerik team

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

  8. Josh Eastburn
    Josh Eastburn avatar
    32 posts
    Member since:
    Aug 2012

    Posted 15 Feb 2012 Link to this post

    Has there been a resolution to this problem?
    I am experiencing the same problem in my application running 2011.2.920.1040.

    Thank you.
  9. Josh Eastburn
    Josh Eastburn avatar
    32 posts
    Member since:
    Aug 2012

    Posted 15 Feb 2012 Link to this post

    To be more precise about the error I am receiving, the exception is due to a NullReferenceException:

    System.InvalidOperationException: Failed to compare two elements in the array. ---> System.NullReferenceException: Object reference not set to an instance of an object.

    The exception is thrown when I try to sort on a column that sorts by a property of a related object that is NULL for an item.  However, I do not get the exception the first time when sorting by this column.  Only after repeatedly changing the sort order on other columns will the error occur.

    The GridViewDataColumn with the problem:
    <telerik:GridViewDataColumn IsReadOnly="True" DataMemberBinding="{Binding Vendoremployee.VendorEmployeeName}" SortMemberPath="Vendoremployee.FirstName" />

    I looked at the data and the Vendoremployee property is null for two of the rows.  But why does this exception not occur the first time I sort on this column?
    Is there a way to change the Sort behavior to trap this exception?
Back to Top