System.NullReferenceException when adding item right after deleting another.

Hey all,

Anybody face this before? When I remove any item but the last one from my Collection (listview) and try to add an item right after that, it fails with System.NullReferenceException. Here's where the debugger points to and the stacktrace. Thx!

Where debugger points to this in code:
**return new ViewCell** { View = new StackLayout { Orientation = StackOrientation.Horizontal, Spacing = 10, Padding = 6, Children = { addressIcon, listLabel } } };

Stacktrace:

Unhandled Exception:

System.NullReferenceException: Object reference not set to an instance of an object.

07-05 21:28:06.643 D/Mono ( 4003): DllImport attempting to load: '/system/lib/liblog.so'.
07-05 21:28:06.643 D/Mono ( 4003): DllImport loaded library '/system/lib/liblog.so'.
07-05 21:28:06.643 D/Mono ( 4003): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
07-05 21:28:06.653 D/Mono ( 4003): Searching for '__android_log_print'.
07-05 21:28:06.653 D/Mono ( 4003): Probing '__android_log_print'.
07-05 21:28:06.653 D/Mono ( 4003): Found as '__android_log_print'.
07-05 21:28:06.663 I/MonoDroid( 4003): UNHANDLED EXCEPTION:
07-05 21:28:06.683 I/MonoDroid( 4003): System.NullReferenceException: Object reference not set to an instance of an object.
07-05 21:28:06.683 I/MonoDroid( 4003): at Xamarin.Forms.Platform.Android.VisualElementPackager.RemoveChild (Xamarin.Forms.VisualElement view) [0x00014] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:139
07-05 21:28:06.683 I/MonoDroid( 4003): at Xamarin.Forms.Platform.Android.VisualElementPackager.OnChildRemoved (System.Object sender, Xamarin.Forms.ElementEventArgs e) [0x0000f] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:125
07-05 21:28:06.693 I/MonoDroid( 4003): at (wrapper delegate-invoke) System.EventHandler1[Xamarin.Forms.ElementEventArgs]:invoke_void_object_TEventArgs (object,Xamarin.Forms.ElementEventArgs) 07-05 21:28:06.693 I/MonoDroid( 4003): at Xamarin.Forms.Element.OnChildRemoved (Xamarin.Forms.Element child) [0x0000f] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:356 07-05 21:28:06.693 I/MonoDroid( 4003): at Xamarin.Forms.VisualElement.OnChildRemoved (Xamarin.Forms.Element child) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:577 07-05 21:28:06.693 I/MonoDroid( 4003): at Xamarin.Forms.Layout1[T].OnChildRemoved (Xamarin.Forms.Element child) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Layout.cs:39
07-05 21:28:06.693 I/MonoDroid( 4003): at Xamarin.Forms.Layout.OnInternalRemoved (Xamarin.Forms.View view) [0x00012] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Layout.cs:412
07-05 21:28:06.693 I/MonoDroid( 4003): at Xamarin.Forms.Layout.InternalChildrenOnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0002f] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Layout.cs:376
07-05 21:28:06.693 I/MonoDroid( 4003): at (wrapper delegate-invoke) :invoke_void_object_NotifyCollectionChangedEventArgs (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
07-05 21:28:06.693 I/MonoDroid( 4003): at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00012] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:288 07-05 21:28:06.693 I/MonoDroid( 4003): at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (NotifyCollectionChangedAction action, System.Object item, Int32 index) [0x00000] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:351
07-05 21:28:06.693 I/MonoDroid( 4003): at System.Collections.ObjectModel.ObservableCollection1[T].RemoveItem (Int32 index) [0x0002b] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:205 07-05 21:28:06.693 I/MonoDroid( 4003): at System.Collections.ObjectModel.Collection1[T].Remove (System.Collections.ObjectModel.T item) [0x0002d] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/collections/objectmodel/collection.cs:113
07-05 21:28:06.693 I/MonoDroid( 4003): at Xamarin.Forms.Layout.OnInternalAdded (Xamarin.Forms.View view) [0x0000b] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Layout.cs:399
07-05 21:28:06.693 I/MonoDroid( 4003): at Xamarin.Forms.Layout.InternalChildrenOnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00089] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Layout.cs:391
07-05 21:28:06.693 I/MonoDroid( 4003): at (wrapper delegate-invoke) :invoke_void_object_NotifyCollectionChangedEventArgs (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
07-05 21:28:06.693 I/MonoDroid( 4003): at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00012] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:288 07-05 21:28:06.693 I/MonoDroid( 4003): at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (NotifyCollectionChangedAction action, System.Object item, Int32 index) [0x00000] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:351
07-05 21:28:06.693 I/MonoDroid( 4003): at System.Collections.ObjectModel.ObservableCollection1[T].InsertItem (Int32 index, System.Collections.ObjectModel.T item) [0x00024] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:219 07-05 21:28:06.693 I/MonoDroid( 4003): at System.Collections.ObjectModel.Collection1[T].Add (System.Collections.ObjectModel.T item) [0x00023] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/collections/objectmodel/collection.cs:67
07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.ObservableWrapper2[TTrack,TRestrict].Add (Xamarin.Forms.TRestrict item) [0x0004b] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ObservableWrapper.cs:35 07-05 21:28:06.703 I/MonoDroid( 4003): at myPT.AddressListView+<>c__DisplayClass4_0.<.ctor>b__0 () [0x0004c] in C:\Users\Ryan J\Documents\Visual Studio 2015\Projects\myPT\myPT\myPT\Views\AddressListView.cs:41 07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.ElementTemplate.CreateContent () [0x00026] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ElementTemplate.cs:78 07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.DataTemplateExtensions.CreateContent (Xamarin.Forms.DataTemplate self, System.Object item, Xamarin.Forms.BindableObject container) [0x00014] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\DataTemplateExtensions.cs:12 07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.TemplatedItemsList2[TView,TItem].CreateContent (Int32 index, System.Object item, Boolean insert) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\TemplatedItemsList.cs:508
07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.TemplatedItemsList2[TView,TItem].ConvertContent (Int32 startingIndex, IList items, Boolean forceCreate, Boolean setIndex) [0x00014] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\TemplatedItemsList.cs:632 07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.TemplatedItemsList2[TView,TItem].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e, Boolean fixWindows) [0x0017e] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\TemplatedItemsList.cs:962
07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.TemplatedItemsList2[TView,TItem].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\TemplatedItemsList.cs:914 07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.ListProxy.OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000a] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ListProxy.cs:229 07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.ListProxy+<>c__DisplayClass32_0.<OnCollectionChanged>b__0 () [0x00018] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ListProxy.cs:202 07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.ListProxy.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x000a0] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ListProxy.cs:221 07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.ListProxy+WeakNotifyProxy.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00031] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ListProxy.cs:385 07-05 21:28:06.703 I/MonoDroid( 4003): at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00012] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:288
07-05 21:28:06.703 I/MonoDroid( 4003): at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (NotifyCollectionChangedAction action, System.Object item, Int32 index) [0x00000] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:351 07-05 21:28:06.703 I/MonoDroid( 4003): at System.Collections.ObjectModel.ObservableCollection1[T].InsertItem (Int32 index, System.Collections.ObjectModel.T item) [0x00024] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:219
07-05 21:28:06.703 I/MonoDroid( 4003): at System.Collections.ObjectModel.Collection`1[T].Add (System.Collections.ObjectModel.T item) [0x00023] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/collections/objectmodel/collection.cs:67
07-05 21:28:06.703 I/MonoDroid( 4003): at myPT.Pages.AddressAdd.AddBtn_Clicked (System.Object sender, System.EventArgs e) [0x000d6] in C:\Users\Ryan J\Documents\Visual Studio 2015\Projects\myPT\myPT\myPT\Pages\AddressAdd.cs:97
07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.Button.Xamarin.Forms.IButtonController.SendClicked () [0x00020] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\Button.cs:122
07-05 21:28:06.703 I/MonoDroid( 4003): at Xamarin.Forms.Platform.Android.ButtonRenderer+ButtonClickListener.OnClick (Android.Views.View v) [0x0000f] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\ButtonRenderer.cs:269
07-05 21:28:06.703 I/MonoDroid( 4003): at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (IntPtr jnienv, IntPtr native__this, IntPtr native_v) [0x00011] in /Users/builder/data/lanes/3415/7db2aac3/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.View.cs:1806
07-05 21:28:06.703 I/MonoDroid( 4003): at (wrapper dynamic-method) System.Object:37af5531-783b-47a6-b78a-63d4a592ed71 (intptr,intptr,intptr)
07-05 21:28:06.773 D/Mono ( 4003): DllImport searching in: '__Internal' ('(null)').
07-05 21:28:06.773 D/Mono ( 4003): Searching for 'java_interop_jnienv_throw'.
07-05 21:28:06.773 D/Mono ( 4003): Probing 'java_interop_jnienv_throw'.
07-05 21:28:06.783 D/Mono ( 4003): Found as 'java_interop_jnienv_throw'.
An unhandled exception occured.

07-05 21:28:08.033 E/mono ( 4003):
07-05 21:28:08.033 E/mono ( 4003): Unhandled Exception:
07-05 21:28:08.033 E/mono ( 4003): System.NullReferenceException: Object reference not set to an instance of an object.
07-05 21:28:08.033 E/mono ( 4003): at (wrapper dynamic-method) System.Object:37af5531-783b-47a6-b78a-63d4a592ed71 (intptr,intptr,intptr)
07-05 21:28:08.033 E/mono ( 4003): at (wrapper native-to-managed) System.Object:37af5531-783b-47a6-b78a-63d4a592ed71 (intptr,intptr,intptr)
In mgmain JNI_OnLoad
07-05 21:28:08.043 E/mono-rt ( 4003): [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
07-05 21:28:08.043 E/mono-rt ( 4003): at (wrapper dynamic-method) System.Object:37af5531-783b-47a6-b78a-63d4a592ed71 (intptr,intptr,intptr)
07-05 21:28:08.043 E/mono-rt ( 4003): at (wrapper native-to-managed) System.Object:37af5531-783b-47a6-b78a-63d4a592ed71 (intptr,intptr,intptr)

Posts

  • JeffGonzalesJeffGonzales USMember ✭✭

    I noticed this in your output:

    07-05 21:28:06.683 I/MonoDroid( 4003): System.NullReferenceException: Object reference not set to an instance of an object.
    07-05 21:28:06.683 I/MonoDroid( 4003): at Xamarin.Forms.Platform.Android.VisualElementPackager.RemoveChild (Xamarin.Forms.VisualElement view) [0x00014] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:139

    I get the same problem when trying to remove Children from my Grid.

    I read this before reading your thread:

    http://forums.xamarin.com/discussion/59788/random-crash-sometimes-when-setting-contentview-content

    Nothing seems to have been fixed.

  • rastapapolisrastapapolis USMember
    edited September 2016

    @JeffGonzales, I resolved this problem many iterations ago. Forgive me for not posting the solution. What happened was when you remove a listview item from the list, it doesn't automatically change the list and update it's indexes. To resolve the issue, I simply reset the ItemsSource with the updated list. Here's the code snippet:

    private void removeSelectedItem(object sender, EventArgs e)
    {
             AddressItem tempItem = (AddressItem)listView.SelectedItem;
    
             listView.viewModel.Addresses.Remove(tempItem);
    
             listView.ItemsSource = listView.viewModel.Addresses;
     } 
    
  • JeffGonzalesJeffGonzales USMember ✭✭

    Although we share the same exception, I don't think we have the same situation. I am trying to remove old elements from a Grid's Children member. It crashes with the same exception you get when I try to remove them. The problem is that I have these Grids in a list. If I don't remove the old ones they keep accumulating until eventually I run out of memory.

  • Ferry.de.BoerFerry.de.Boer NLUniversity ✭✭
    edited October 2016

    Similar problem here. It's quite annoying since it seems to be nondeterministic. I don't know about the other cases but for me it happens in a Cell. So I'm modifying a list of values in a stack layout while the cell is also being moved in the list. I tried recreating the StackLayout but then I get the same exception from the Childrens list I'm readding the stacklayout with it's new items. It doesn't really matter what operations you're doing on the children.

    It is however costing me quite some time and headaches by now to a point where I'm seriously starting to doubt ditching Forms altogether. I'm not that far in building the app. The longer I'm fed up with this the more expensive it's going to be and I will have an unsatisfied customer and users. :disappointed:

  • IgnasBagdonasIgnasBagdonas LTMember
    edited February 28

    I get this error on Children.Clear(); in Layout<View>.

    Object reference not set to an instance of an object.
    at Xamarin.Forms.Platform.Android.VisualElementPackager.RemoveChild

    Any solutions or fixes?

  • GnanaprakashChandiranGnanaprakashChandiran USMember ✭✭

    i am also get same error.

    Object reference not set to an instance of an object.
    at Xamarin.Forms.Platform.Android.VisualElementPackager.RemoveChild

    Anyone find solution for this?

  • QwinQwin USUniversity ✭✭
    edited March 8

    Yup getting the same on Children.Clear() @TheRealJasonSmith @StephaneDelcroix any ideas guys ?

    looking at the code in VisualElementPackager.cs (line 139 thats where it says nullpointerexception, RemoveChild) I see that there is a null check. So how can this be ?

        void OnChildRemoved(object sender, ElementEventArgs e)
        {
            Performance.Start();
            var view = e.Element as VisualElement;
            if (view != null)
                RemoveChild(view);
    
            Performance.Stop();
        }
    
  • schnitzelditzelschnitzelditzel DEMember

    I had the same problem. By defining the ItemTemplate in C# and not in XAML the error was gone.

    My XAML looks like this
    <ListView x:Name="TimersList" ItemsSource="{Binding CurrentMachine.Timer}" />

    The rest ist in C#.

  • HMAKHMAK USMember ✭✭

    Same here, I am getting System.NullReferenceException when I try to add an item to a list, which sounds very basic:

    This is my code:
    city_street_points.Add(cstreet_point2);

    city_street_points is a list of RoadwayPoint object, cstreet_point2 is a Roadway object that is not null..

    Any idea how to resolve that?

Sign In or Register to comment.