Bug 22319 - Null exception thrown in StackLayout
Summary: Null exception thrown in StackLayout
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.2.2
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2014-08-22 20:15 UTC by Paul Johnson
Modified: 2015-01-15 17:01 UTC (History)
5 users (show)

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

Source for reproducing the crash (2.95 MB, application/zip)
2014-08-22 20:15 UTC, Paul Johnson

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 on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Paul Johnson 2014-08-22 20:15:07 UTC
The attached source shows the problem.

The code compiles without an issue and executes. However, when it hits line 29 in App.cs (Children = {top, scrlView}), a System.NullReferenceException is thrown claiming stackView is null.

System.NullReferenceException: Object reference not set to an instance of an object
  at at Xamarin.Forms.NavigationProxy.set_Inner (Xamarin.Forms.INavigation) <IL 0x00076, 0x00334>
  at at Xamarin.Forms.VisualElement.OnParentSet () <IL 0x0001f, 0x000fb>
  at at Xamarin.Forms.Element.set_Parent (Xamarin.Forms.Element) <IL 0x00026, 0x0013b>
  at at Xamarin.Forms.Element.OnChildAdded (Xamarin.Forms.Element) <IL 0x00002, 0x00053>
  at at Xamarin.Forms.Layout.OnInternalAdded (Xamarin.Forms.View) <IL 0x00014, 0x000f3>
  at at Xamarin.Forms.Layout.InternalChildrenOnCollectionChanged (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs) <IL 0x00070, 0x004bb>
  at at (wrapper delegate-invoke) <Module>.invoke_void_object_NotifyCollectionChangedEventArgs (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs) <IL 0x00027, 0x00067>
  at at (wrapper delegate-invoke) <Module>.invoke_void_object_NotifyCollectionChangedEventArgs (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs) <IL 0x00027, 0x00067>
  at at System.Collections.ObjectModel.ObservableCollection`1<Xamarin.Forms.Element>.OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs) <0x000f7>
  at at System.Collections.ObjectModel.ObservableCollection`1<Xamarin.Forms.Element>.InsertItem (int,Xamarin.Forms.Element) <0x000d3>
  at at System.Collections.ObjectModel.Collection`1<Xamarin.Forms.Element>.Add (Xamarin.Forms.Element) <0x000c3>
  at at Xamarin.Forms.ObservableWrapper`2<Xamarin.Forms.Element, Xamarin.Forms.View>.Add (Xamarin.Forms.View) <0x00203>
  at StackView.App.GetMainPage () [0x00083] in /Volumes/Developer/Developer/StackView/StackView/App.cs:29
  at StackView.Android.MainActivity.OnCreate (Android.OS.Bundle) [0x00010] in /Volumes/Developer/Developer/StackView/Android/MainActivity.cs:25
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/1148/fd6fa476/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:2423
  at at (wrapper dynamic-method) object.d8797bec-c6ba-4fc5-91b1-fb7ec836e77b (intptr,intptr,intptr) <IL 0x00017, 0x00043>
Comment 1 Paul Johnson 2014-08-22 20:15:41 UTC
Created attachment 7780 [details]
Source for reproducing the crash
Comment 2 Akhilesh kumar 2014-08-25 05:38:08 UTC
I have checked this issue with attached sample and I am also observing that on running the application for android or iOS, it throws NRE at line 29 in App.cs. 

Exception details: https://gist.github.com/AkhileshKumar01/7b6423e2cb6b66995239

Environment info: 
=== Xamarin Studio ===

Version 5.3 (build 434)
Installation UUID: ff0c16c6-3c75-46d8-ac56-56c3b56e2c76
	Mono 3.8.0 ((no/62a857e)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 308000007

=== Apple Developer Tools ===

Xcode 5.1.1 (5085)
Build 5B1008

=== Xamarin.Mac ===

Version: (Starter Edition)

=== Xamarin.Android ===

Version: 4.16.0 (Trial Edition)
Android SDK: /Users/360_macmini/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		3.2    (API level 13)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		4.5    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin.iOS ===

Version: (Trial Edition)
Hash: 3991e99
Build date: 2014-08-22 13:09:34-0400

=== Build Information ===

Release ID: 503000434
Git revision: 9b508e2173c6a211fa020d9743a86b484b5ed0d2
Build date: 2014-08-21 12:41:28-04
Xamarin addins: 69708b283b171ea053d0edfbf09d98457d21fbee

=== Operating System ===

Mac OS X 10.9.4
Darwin Apples-iMac.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 3 Chase Florell 2014-08-25 11:02:13 UTC
Sorry but this is NOT a bug.

1) The null ref ex is because you're adding Navigation.PopAsync without a NavigationPage.... So Navigation is null

2) once I remove that bit from your code, the next exception is due to it not being able to find the resource you're requesting... probably the image.
Comment 4 Chase Florell 2014-08-25 11:02:47 UTC
How did this get "Confirmed"?
Comment 5 Paul Johnson 2014-08-25 11:18:14 UTC
I've removed the two calls to the Navigation.PopAsync and Navigation.PopToRootAsync for gridButtonBack and gridButtonHome and still get the same crash of a null exception at line 29. 

Correcting the filenames to icoback.png and icohome.png still gets the NRE at line 29

Looking at the gridButtonBack object, the image filename is the filename passed in, though it does not seem qualified to include the Android internal path (unless Xam.Forms does some magic or other to add them at runtime)
Comment 6 Chase Florell 2014-08-25 11:21:31 UTC
This is not a support channel. Your code needs some help, but it's not a bug in the Xamarin.Forms platform. If you need support, please make your way over to

 1. http://forums.xamarin.com/categories/xamarin-forms
 2. https://store.xamarin.com/account/my/subscription (and send an email)
Comment 7 Paul Johnson 2014-08-25 11:29:03 UTC
Chase - I know this is not a support channel, I've been using it for long enough! 

All I am seeing though is an NRE being thrown with very little in the way to point to why the NRE is being thrown with two objects, neither of which have anything obviously wrong in them. I could understand it if (say) top was null and then I wouldn't have reported it as it's obviously an issue on my side.

I shall however take your advice and ask on the forums.
Comment 8 Paul Johnson 2015-01-15 17:01:23 UTC
Not  a bug