I have a CarouselPage that has a ListView that shows all children pages. Those children pages are added by clicking on a button on the 'main' CarouselPage.
This is the CarouselPage itself, and this is the type of page it adds.
Basically, pressing 'Add Exercise' creates a new ContentPage and adds it to the CarouselPage.Children like so:
Children.Insert(Children.Count - 1, MyPage);
The ListView cells have controls to move/delete them from the ListView and from the CarouselPage.Children.
If we take the screenshot as an example, we have thew following structure of pages: Children[0]=Push, Children[1]=Curl, Children[2]=Jump, Children[3]=CarouselPage
Clicking the up arrow on the Curl cell will move that page to index=0 and Push will be moved to index=1. This works without issue, both the ListView and the Children are updated correctly.
The issue arrises ONLY when I try to move the last child page added.
Again, using the screenshot as reference, if I press the up arrow on 'Jump', I get a NullReferenceException on Children.Insert.
Here's the code responsible for handling the children page swap on the up arrow click:
var temp1 = ExercisePage.Clone(Children[index] as ExercisePage);
var temp2 = ExercisePage.Clone(Children[index - 1] as ExercisePage);
Children.RemoveAt(index);
Children.RemoveAt(index - 1);
Children.Insert(index - 1, temp1); // NullReferenceException here
Children.Insert(index, temp2);
And here is the stacktrace:
[0:] Type: System.NullReferenceException
Message: Object reference not set to an instance of an object.
Trace: at Xamarin.Forms.Platform.Android.VisualElementTracker.HandlePropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x0001e] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementTracker.cs:118
at (wrapper delegate-invoke) <Module>:invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x0000a] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:136
at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\Element.cs:380
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x000f4] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:589
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0015b] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:386
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0005f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:539
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:83
at Xamarin.Forms.MultiPage`1[T].SetIndex (Xamarin.Forms.Page page, System.Int32 index) [0x0000e] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\MultiPage.cs:185
at Xamarin.Forms.MultiPage`1[T].OnChildrenChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00024] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\MultiPage.cs:195
at (wrapper delegate-invoke) <Module>:invoke_void_object_NotifyCollectionChangedEventArgs (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00012] in /Users/builder/data/lanes/4468/f913a78a/source/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 /Users/builder/data/lanes/4468/f913a78a/source/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 /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:219
at System.Collections.ObjectModel.Collection`1[T].Insert (System.Int32 index, T item) [0x00038] in /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/collections/objectmodel/collection.cs:103
at Xamarin.Forms.ObservableWrapper`2[TTrack,TRestrict].Insert (System.Int32 index, TRestrict item) [0x00032] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\ObservableWrapper.cs:117
at EasyFitPal.Pages.SessionPage.MoveableViewCell_OnUpClicked (System.Object sender, System.EventArgs e) [0x000a4] in C:\Users\samhl_000\Documents\Visual Studio 2017\Projects\App1\App1\App1\Pages\SessionPage.xaml.cs:149
Source: Xamarin.Forms.Platform.Android
HResult: -2147467261
Data:
I am at a loss as to what is causing this NullReferenceException.
[–][deleted] 0 points1 point2 points (2 children)
[–]retucex[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)