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

Sign In or Register to comment.