ListView NullReferenceException when reseting / updating ItemSource

WammyWammy USMember ✭✭

In my ViewModel I have a property called 'Objects' which is an ObservableCollection.

When I setup the ListView it is as such:

ObjectList = new ListView()
{
        IsPullToRefreshEnabled = true,
        HasUnevenRows = ViewModel.HasUnevenRows,
        RefreshCommand = ViewModel.LoadObjects,
        ItemsSource = ViewModel.Objects,
        ItemTemplate = new DataTemplate(ViewModel.ListViewItem),
        BindingContext = ViewModel
};
ObjectList.SetBinding(ListView.IsRefreshingProperty, new Binding("IsLoading"));

My ViewModel has an IsLoading property and I am making sure I'm not reloading the list while it is already being loaded via the command CanExecute:

LoadObjects = new Command(() => { ListObjects(); },() => { return !IsLoading; });

When I pull to refresh it executes the LoadObjects command.

The issue I am encountering is when I pull to refresh the list, after the 3rd or 4th time it will crash with this:

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at Xamarin.Forms.Platform.iOS.LabelRenderer.UpdateText () [0x00070] in <8f7e3b9d1483403590c8ab3d78cce1ac>:0 
  at Xamarin.Forms.Platform.iOS.LabelRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00097] in <8f7e3b9d1483403590c8ab3d78cce1ac>:0 
  at (wrapper delegate-invoke) <Module>:invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
  at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x0000a] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:137 
  at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in C:\agent\_work\3\s\Xamarin.Forms.Core\Element.cs:388 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x000f4] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:593 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0015b] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:390 
  at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x001f9] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindingExpression.cs:174 
  at Xamarin.Forms.BindingExpression.Apply (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property) [0x0006b] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindingExpression.cs:77 
  at Xamarin.Forms.Binding.Apply (System.Object newContext, Xamarin.Forms.BindableObject bindObj, Xamarin.Forms.BindableProperty targetProperty) [0x00042] in C:\agent\_work\3\s\Xamarin.Forms.Core\Binding.cs:126 
  at Xamarin.Forms.BindableObject.ApplyBindings (System.Boolean skipBindingContext) [0x0003b] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:421 
  at Xamarin.Forms.BindableObject.ApplyBindings () [0x00000] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:125 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x0005a] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:119 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in C:\agent\_work\3\s\Xamarin.Forms.Core\Element.cs:498 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in C:\agent\_work\3\s\Xamarin.Forms.Core\Element.cs:341 
  at Xamarin.Forms.View.OnBindingContextChanged () [0x00042] in C:\agent\_work\3\s\Xamarin.Forms.Core\View.cs:99 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x00060] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:120 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in C:\agent\_work\3\s\Xamarin.Forms.Core\Element.cs:498 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in C:\agent\_work\3\s\Xamarin.Forms.Core\Element.cs:341 
  at Xamarin.Forms.View.OnBindingContextChanged () [0x00042] in C:\agent\_work\3\s\Xamarin.Forms.Core\View.cs:99 
  at Xamarin.Forms.BindableObject.SetInheritedBindingContext (Xamarin.Forms.BindableObject bindable, System.Object value) [0x00060] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:120 
  at Xamarin.Forms.Element.SetChildInheritedBindingContext (Xamarin.Forms.Element child, System.Object context) [0x00000] in C:\agent\_work\3\s\Xamarin.Forms.Core\Element.cs:498 
  at Xamarin.Forms.Element.OnBindingContextChanged () [0x00021] in C:\agent\_work\3\s\Xamarin.Forms.Core\Element.cs:341 
  at Xamarin.Forms.Cell.OnBindingContextChanged () [0x00000] in C:\agent\_work\3\s\Xamarin.Forms.Core\Cells\Cell.cs:114 
  at Xamarin.Forms.BindableObject.BindingContextPropertyChanged (Xamarin.Forms.BindableObject bindable, System.Object oldvalue, System.Object newvalue) [0x0000e] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:441 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00108] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:596 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0015b] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:390 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0005f] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:543 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:83 
  at Xamarin.Forms.BindableObject.set_BindingContext (System.Object value) [0x00000] in C:\agent\_work\3\s\Xamarin.Forms.Core\BindableObject.cs:24 
  at Xamarin.Forms.Internals.TemplatedItemsList`2+<UnhookItem>d__154[TView,TItem].MoveNext () [0x000b0] in C:\agent\_work\3\s\Xamarin.Forms.Core\TemplatedItemsList.cs:1217 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.2.0.11/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.2.0.11/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 
  at UIKit.UIKitSynchronizationContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/5481/2f8bbec0/source/xamarin-macios/src/UIKit/UIKitSynchronizationContext.cs:24 
  at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/5481/2f8bbec0/source/xamarin-macios/src/Foundation/NSAction.cs:163 
--- End of stack trace from previous location where exception was thrown ---
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/5481/2f8bbec0/source/xamarin-macios/src/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/5481/2f8bbec0/source/xamarin-macios/src/UIKit/UIApplication.cs:63 
  at HydrusCRM.iOS.Application.Main (System.String[] args) [0x00001] in /Volumes/Data/Apps/HydrusCRM/iOS/Main.cs:17 

Here is the code that updates the objects collection:

var result = (IList)JsonConvert.DeserializeObject(results, listType);
if (result.Count > 0)
{
    Device.BeginInvokeOnMainThread(() =>
    {
        foreach (Models.Objects.Model @object in result)
        {
        objects.Add(@object);
    }
    });
}

I am not sure where the issue is coming from as none of my apps code is in the exception stack.

Tagged:

Best Answer

Answers

  • WammyWammy USMember ✭✭

    One thing I forgot to mention, is that I am calling objects.Clear(); towards the beginning of ListObjects() after setting IsLoading=true

  • WammyWammy USMember ✭✭

    @PaulDiPietro Looks like you are correct. Reading through the bug report it seems that setting HasUnevenRows to true triggers this bug. I've set it to false and set the RowHeight Property on the ListView and the crash no longer happens.

  • hardikphdhardikphd INMember

    Same workaround worked for me too.
    HasUnevenRows to false and RowHeight set manually

  • D4rkBLockD4rkBLock USMember

    @Wammy Hello. I have this problem too and i found a way to cheat. You have to create a CustomLabelRenderer like this:

    [assembly: ExportRenderer(handler: typeof(Xamarin.Forms.Label), target: typeof(CustomLabelRenderer))]
    namespace ITL.Mobile.iOS.Renderers
    {
    public class CustomLabelRenderer : LabelRenderer
    {
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Label> e)
    {
    base.OnElementChanged(e);
    }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (Control != null)
            {
                base.OnElementPropertyChanged(sender, e);
            }
        }
    }
    

    }

    The problem are on OnElementPropertyChanged method, for some reason, Control comes null, causing the problem when Xamarin try to update the text/textcolor property

  • marcnegrimarcnegri CHMember ✭✭

    Hi everyone,

    I have exactly the same issue... but i'm still stuck... this is really a huge bug...
    @D4rkBLock I'm trying your workaround, not working for me. But it make sense. My Row are complex custom components, so I'll try just to simplify rows and test again... wait and see.

  • marcnegrimarcnegri CHMember ✭✭

    Hi I noticed that is my Image which crash ios plateform. if I remove images from my custom component, it's working. I'll try to create a CustomImageRenderer overriding OnElementPropertyChanged and OnElementChanged... but unfortunately, it's not working...

    System.NullReferenceException: Object reference not set to an instance of an object
    at Xamarin.Forms.Platform.iOS.EventTracker.LoadRecognizers () [0x0005d] in :0
    at Xamarin.Forms.Platform.iOS.EventTracker.OnElementChanged (System.Object sender, Xamarin.Forms.Platform.iOS.VisualElementChangedEventArgs e) [0x0004e] in :0
    at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs1[TElement] e) [0x0002c] in :0
    at Xamarin.Forms.Platform.iOS.ViewRenderer2[TView,TNativeView].OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs1[TElement] e) [0x00000] in :0
    at Xamarin.Forms.Platform.iOS.ImageRenderer.<>n__0 (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) [0x00000] in :0
    at Xamarin.Forms.Platform.iOS.ImageRenderer+d__2.MoveNext () [0x000d6] in :0
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152

  • ZoliZoli NLMember ✭✭

    @marcnegri said:
    Hi I noticed that is my Image which crash ios plateform.

    The issue is also with Label (in a listview), if it has TapGestureRecognizer.
    It will crash when modifying the collection in IOS.
    If I remove the Label.TapGestureRecognizer, no crash, it works fine - but I need to detect if user clicks on a label in the listview. This is now a long standing un-fixed serious issue in Xamarin :(

Sign In or Register to comment.