0

Hi tried to compile my app for the first time for IOS but I got this:

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

in the AppDelegate. It stops in this line :return base.FinishedLaunching(app, options);

I'm relative new into xamarin so I am sorry for my unknowingness.

Here is my complete AppDelegate:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Globalization;
using System.IO;
using Flex;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using System.Xml.Linq;

namespace dpsApp.iOS
{

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {



        global::Xamarin.Forms.Forms.Init();
        Rg.Plugins.Popup.Popup.Init();
        FlexButton.Init();
        LoadApplication(new App());

        return base.FinishedLaunching(app, options);
    }
}

}

So here is my MainPage XAML: `

        <StackLayout WidthRequest="10000">
            <StackLayout x:Name="LogoStack" BackgroundColor="White">
                <Image x:Name="Image" 
                        Source="a.png"
                        HeightRequest="120"
                        WidthRequest="120"
                        HorizontalOptions="Center">

                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Tapped="EasterCount"/>
                    </Image.GestureRecognizers>
                </Image>

            </StackLayout>

            <StackLayout x:Name="StackList"  IsVisible="True" HeightRequest="3000">
                <ListView x:Name="PageList" 
                      HasUnevenRows="True"
                      ItemTapped="Link_ItemTapped"
                      HeightRequest="25"
                      BackgroundColor="White">

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Orientation="Horizontal" Margin="0,0,0,0" BackgroundColor="#e7e7e7" HeightRequest="65" Padding="0">

                                    <Grid x:Name="DeleteStack" VerticalOptions="CenterAndExpand" BackgroundColor="White" HorizontalOptions="FillAndExpand" >
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="50" />
                                        </Grid.ColumnDefinitions>

                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="28" />
                                            <RowDefinition Height="22" />
                                            <RowDefinition Height="1" />
                                        </Grid.RowDefinitions>

                                        <Label Grid.Column="0" Grid.Row="0" Text="{Binding Title}" LineBreakMode="TailTruncation" FontSize="25" Margin="20,0,0,0"/>
                                        <Label Grid.Column="0" Grid.Row="1" Text="{Binding Link}" LineBreakMode="TailTruncation" FontSize="17" Margin="20,0,0,0"/>

                                        <Image Margin="0,0,20,0"  
                                                   IsVisible="{Binding IsVisible}" 
                                                   Grid.Column="1" 
                                                   Grid.Row="0" 
                                                   Grid.RowSpan="2" 
                                                   x:Name="DeleteButton"                                                        
                                                   Source="delete.png" 
                                                   VerticalOptions="Center"
                                                   HeightRequest="20" 
                                                   HorizontalOptions="Center"/>

                                    </Grid>

                                </StackLayout>
                            </ViewCell>

                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </StackLayout>
            <StackLayout x:Name="FirstTimeOpenStack" HorizontalOptions="Center">
                <Label Text="Please tap on the plus icon in the top right corner to add a website" />

            </StackLayout>

        </StackLayout>
6
  • may be you have something wrong in your xaml design for launching page, can you share your code for better idea, or else try to debug and see output logs for more detail Commented Jun 19, 2018 at 12:21
  • The problem is that it works perfectly on android Commented Jun 19, 2018 at 12:24
  • That is not a problem. On Android the initialization is different. Can you share more code than the exception? Commented Jun 19, 2018 at 12:48
  • Which code do you mean? I added my complete AppDelegate. Commented Jun 19, 2018 at 12:52
  • @Markus3D Can you share your main page XAML (the landing page ,after your app started) ? Seems you have an issue in XAML of particular page and an exception is thrown to App delegate . Commented Jun 20, 2018 at 3:27

1 Answer 1

2

Your App Delegate looks ok. You have an unhandled exception somewhere in the App() you are launching.

Here is one way to capture unhandled exceptions:

namespace WiFiVisualPager.iOS
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the 
    // User Interface of the application, as well as listening (and optionally responding) to 
    // application events from iOS.
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
            TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;

            global::Xamarin.Forms.Forms.Init();
            DisplayCrashReport();

            LoadApplication(new App());

            return base.FinishedLaunching(app, options);
        }

        #region [Error handling]
        //Credit: Peter Norman.
        //https://peterno.wordpress.com/2015/04/15/unhandled-exception-handling-in-ios-and-android-with-xamarin/
        //Minor compile fixes by David McCurley.

        private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
        {
            var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception);
            LogUnhandledException(newExc);
        }

        private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
        {
            var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception);
            LogUnhandledException(newExc);
        }

        internal static void LogUnhandledException(Exception exception)
        {
            try
            {
                const string errorFileName = "Fatal.log";
                var libraryPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Resources); // iOS: Environment.SpecialFolder.Resources
                var errorFilePath = Path.Combine(libraryPath, errorFileName);
                var errorMessage = String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}",
                DateTime.Now, exception.ToString());
                File.WriteAllText(errorFilePath, errorMessage);

                // Log to Android Device Logging.
                //Android.Util.Log.Error("Crash Report", errorMessage);
            }
            catch
            {
                // just suppress any error logging exceptions
            }
        }

        /// <summary>
        // If there is an unhandled exception, the exception information is diplayed 
        // on screen the next time the app is started (only in debug configuration)
        /// </summary>
        [Conditional("DEBUG")]
        private static void DisplayCrashReport()
        {
            const string errorFilename = "Fatal.log";
            var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources);
            var errorFilePath = Path.Combine(libraryPath, errorFilename);

            if (!File.Exists(errorFilePath))
            {
                return;
            }

            var errorText = File.ReadAllText(errorFilePath);
            var alertView = new UIAlertView("Crash Report", errorText, null, "Close", "Clear") { UserInteractionEnabled = true };
            alertView.Clicked += (sender, args) =>
            {
                if (args.ButtonIndex != 0)
                {
                    File.Delete(errorFilePath);
                }
            };
            alertView.Show();
        }
        #endregion
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but this doesn't help me at all :) In my last line of FinishedLaunching : return base.FinishedLaunching(app, options); the parameter options is null I dont know if this is right :D
Options is defined above as "NSDictionary options", which is passed in by the framework. I highly doubt that's the issue. Did you try adding the crash handler code? If so, what's the stacktrace?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.