Bug 60734 - ListView ObservableCollection clear throws null reference exception
Summary: ListView ObservableCollection clear throws null reference exception
Status: NEEDINFO
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 2.5.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on: 60966
Blocks:
  Show dependency tree
 
Reported: 2017-11-17 16:17 UTC by Vladimir
Modified: 2018-03-04 20:12 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Observable Collections throws null reference (138.67 KB, application/zip)
2018-03-04 20:12 UTC, Ronald van der Veen
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 60734 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEEDINFO

Description Vladimir 2017-11-17 16:17:06 UTC
I have such structure of the xaml page

<ListView x:Name="List"
                  ItemsSource="{Binding Items}"
                  Style="{StaticResource ListStyle}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                            <StackLayout Orientation="Horizontal">
                                <StackLayout.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding Path=BindingContext.ItemDetailCommand, Source={x:Reference Name=List}}"
                                                          CommandParameter="{Binding .}"
                                                          NumberOfTapsRequired="1"/>
                                </StackLayout.GestureRecognizers>
                                <Label />
                            </StackLayout>
                        </Frame>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

And when I call Clear() on ObservableCollection, null reference exception is thrown
Here is stack trace

  at Xamarin.Forms.Platform.iOS.EventTracker.LoadRecognizers () [0x0005d] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\EventTracker.cs:373 
  at Xamarin.Forms.Platform.iOS.EventTracker.OnElementChanged (System.Object sender, Xamarin.Forms.Platform.iOS.VisualElementChangedEventArgs e) [0x0004e] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\EventTracker.cs:439 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) [0x00020] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:279 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x00110] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:189 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00000] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:140 
  at Xamarin.Forms.Platform.iOS.RendererPool.UpdateRenderers (Xamarin.Forms.Element newElement) [0x0008c] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\RendererPool.cs:157 
  at Xamarin.Forms.Platform.iOS.RendererPool.UpdateNewElement (Xamarin.Forms.VisualElement newElement) [0x00080] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\RendererPool.cs:78 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00050] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:161 
  at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnRendererElementChanged (System.Object sender, Xamarin.Forms.Platform.iOS.VisualElementChangedEventArgs args) [0x0000f] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:144 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) [0x00020] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:279 
  at Xamarin.Forms.Platform.iOS.FrameRenderer.OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) [0x00000] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\Renderers\FrameRenderer.cs:11 
  at GearEye.iOS.Renderers.MaterialFrameRenderer.OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) [0x00001] in D:\GearEye\GearEye\GearEye\GearEye.iOS\Renderers\MaterialFrameRenderer.cs:14 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x00110] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:189 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00000] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:140 
  at Xamarin.Forms.Platform.iOS.ListViewRenderer+UnevenListViewDataSource.CalculateHeightForCell (UIKit.UITableView tableView, Xamarin.Forms.Cell cell) [0x00035] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:745 
  at Xamarin.Forms.Platform.iOS.ListViewRenderer+UnevenListViewDataSource.GetEstimatedRowHeight (UIKit.UITableView table) [0x00095] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:689 
  at Xamarin.Forms.Platform.iOS.ListViewRenderer.UpdateEstimatedRowHeight () [0x0005f] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:399 
  at Xamarin.Forms.Platform.iOS.ListViewRenderer.UpdateItems (System.Collections.Specialized.NotifyCollectionChangedEventArgs e, System.Int32 section, System.Boolean resetWhenGrouped) [0x000bc] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:527 
  at Xamarin.Forms.Platform.iOS.ListViewRenderer.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00000] in D:\agent\_work\2\s\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:298 
  at Xamarin.Forms.Internals.TemplatedItemsList`2[TView,TItem].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000a] in D:\agent\_work\2\s\Xamarin.Forms.Core\TemplatedItemsList.cs:771 
  at Xamarin.Forms.Internals.TemplatedItemsList`2[TView,TItem].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e, System.Boolean fixWindows) [0x0047b] in D:\agent\_work\2\s\Xamarin.Forms.Core\TemplatedItemsList.cs:1114 
  at Xamarin.Forms.Internals.TemplatedItemsList`2[TView,TItem].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00000] in D:\agent\_work\2\s\Xamarin.Forms.Core\TemplatedItemsList.cs:968 
  at Xamarin.Forms.ListProxy.OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000a] in D:\agent\_work\2\s\Xamarin.Forms.Core\ListProxy.cs:233 
  at Xamarin.Forms.ListProxy+<>c__DisplayClass33_0.<OnCollectionChanged>b__0 () [0x00018] in D:\agent\_work\2\s\Xamarin.Forms.Core\ListProxy.cs:206 
  at Xamarin.Forms.ListProxy.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x000a0] in D:\agent\_work\2\s\Xamarin.Forms.Core\ListProxy.cs:225 
  at Xamarin.Forms.ListProxy+WeakNotifyProxy.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00031] in D:\agent\_work\2\s\Xamarin.Forms.Core\ListProxy.cs:394 
  at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000f] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:288 
  at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedAction action, System.Object item, System.Int32 index) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:351 
  at System.Collections.ObjectModel.ObservableCollection`1[T].InsertItem (System.Int32 index, T item) [0x00024] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:219 
  at System.Collections.ObjectModel.Collection`1[T].Add (T item) [0x00020] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/mscorlib/system/collections/objectmodel/collection.cs:67
Comment 1 Paul DiPietro [MSFT] 2017-11-20 23:03:00 UTC
Can you please attach a full reproduction project? This seems to work in plain code with a simple gesture recognizer from what I can tell, but I'd like to see what you're binding those items to.
Comment 2 Vladimir 2017-11-21 09:00:39 UTC
unfortunately, I can't share with you full project, but here is what items binding to

public ObservableCollection<BindableItemModel> Items

public class BindableItemModel : BindableBase
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Notes { get; set; }

        private DateTime? foundDate;

        public DateTime? FoundDate
        {
            get => this.foundDate;
            set
            {
                this.SetProperty(ref this.foundDate, value, nameof(this.FoundDate));
                this.RaisePropertyChanged(nameof(this.ItemScanStatus));
            }
        }

        private bool wasFound;

        public bool WasFound
        {
            get => this.wasFound;
            set
            {
                this.SetProperty(ref this.wasFound, value, nameof(this.WasFound));
                this.RaisePropertyChanged(nameof(this.ItemScanStatus));
            }
        }

        private ItemStatus itemStatus;

        public ItemStatus ItemStatus
        {
            get => this.itemStatus;
            set => this.SetProperty(ref this.itemStatus, value, nameof(this.ItemStatus));
        }

        public string ImagePath { get; set; }

        private bool isTracked;

        public bool IsTracked
        {
            get => this.isTracked;
            set
            {
                this.SetProperty(ref this.isTracked, value, nameof(this.IsTracked));
                this.RaisePropertyChanged(nameof(this.ItemScanStatus));
            }
        }

        private bool isScanning;

        public bool IsScanning
        {
            get => this.isScanning;
            set
            {
                this.SetProperty(ref this.isScanning, value, nameof(this.IsScanning));
                this.RaisePropertyChanged(nameof(this.ItemScanStatus));
            }
        }

        public string ItemScanStatus
        {
            get
            {
                if (!this.IsTracked)
                {
                    return "Not being tracked";
                }

                if (this.IsScanning)
                {
                    return "Scanning...";
                }

                if (!this.WasFound && !this.FoundDate.HasValue)
                {
                    return string.Empty;
                }

                if (this.WasFound && this.FoundDate.HasValue)
                {
                    return $"last seen {this.FoundDate.Value:mm/dd/yy} at PLACE";
                }

                if (!this.WasFound && this.FoundDate.HasValue)
                {
                    return "missing, last seen at PLACE";
                }

                return "Missing";
            }
        }
    }
Comment 3 Ronald van der Veen 2018-03-04 20:12:22 UTC
Created attachment 26201 [details]
Observable Collections throws null reference

Hi there, 
I stumbled to the same issue mentioned above. 

This attachment is the Visual Studio default File New Solution. I just added GestureRecognizer to one of the labels in the XAML. 
(We use similar functionality added to a ‘IconLabel’ to create some sort of custom CheckBox within a ListView.)

<Label.GestureRecognizers>
	<TapGestureRecognizer Command="{Binding LoadItemsCommand}" />
</Label.GestureRecognizers> 

To reproduce, just Pull-To-Refresh...

Nb. disabling Items.Clear(); in the ExecuteLoadItemsCommand no exception was thrown.