Bug 28952 - NullReferenceException when setting MainPage for first time in OnStart
Summary: NullReferenceException when setting MainPage for first time in OnStart
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 1.4.2
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Eric Maupin
Depends on:
Reported: 2015-04-09 17:09 UTC by Adam Kemp
Modified: 2015-06-18 07:11 UTC (History)
4 users (show)

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

Test case (175.70 KB, application/zip)
2015-04-09 17:09 UTC, Adam Kemp

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 Adam Kemp 2015-04-09 17:09:28 UTC
Created attachment 10694 [details]
Test case

The FormsApplicationDelegate class's FinishedLaunching method calls SetMainPage to set window's RootViewController and make the window key and visible. UpdatePage (called when the MainPage property of the application changes) also sets the window's root view controller, but it does an additional step of disposing the old root view controller.

As a result, ff you don't set the MainPage until after FinishedLaunching then UpdatePage crashes with a NullReferenceException when trying to dispose the old (null) root view controller.

SetMainPage should just call UpdateMainPage so that these two functions do the same thing, and then UpdateMainPage should check for null before trying to dispose the old view controller.

To reproduce run the attached project.
Comment 1 Adam Kemp 2015-04-09 17:11:55 UTC
FYI, I know that it's expected to set the MainPage in the app's constructor, but I can't think of a technical reason that setting it in OnStart instead shouldn't work as expected.
Comment 2 Hines Vaughan III 2015-04-27 10:50:43 UTC
I am also getting the same error using Xamarin Forms Thank you.
Comment 3 Jason Smith [MSFT] 2015-05-27 16:10:57 UTC
Should be fixed in 1.4.3 final
Comment 4 Parmendra Kumar 2015-06-18 07:11:11 UTC
I have checked this issue with xamarin.forms. and its working fine at my end.

Screencast: http://www.screencast.com/t/yW1ik6wSAy

Hence closing this issue.

Environment info:

Xamarin Studio 5.9.3 (build 1)
Installation UUID: 1a096c6f-0678-402e-89b2-a2c10f7e80e4
Mono 4.0.1 ((detached/ed1d3ec)
GTK+ 2.24.23 (Raleigh theme)
Package version: 400010044
Xcode 6.2 (6776)
Build 6C131e
Xamarin.iOS : (Enterprise Edition)
Xamarin.Android : (Enterprise Edition)
Xamarin.Mac : (Enterprise Edition)
Mac OS X 10.9.4