Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS Crashlytics - NullReferenceException - Object reference not set to an instance of an object. #424

Open
richaplinvs opened this issue Mar 3, 2025 · 1 comment

Comments

@richaplinvs
Copy link

I am getting an exception accessing any of the Crashlytics methods on iOS. I am also using Analytics too but that is working ok.

The exceptions take the following form:

NullReferenceException - Object reference not set to an instance of an object. 
  at Plugin.Firebase.Crashlytics.FirebaseCrashlyticsImplementation.Log(String message)
NullReferenceException - Object reference not set to an instance of an object.
  at Plugin.Firebase.Crashlytics.FirebaseCrashlyticsImplementation.DidCrashOnPreviousExecution()

I've had quite a few issues inconsistent build and runtime errors integrating Crashlytics. I ran into the issue with the ObjCRuntime.__Registrar__ but uses the static registrar workaround. I tried the latest dotnet (9.0.200) as this should have fixed the underlying issue and avoided the workaround but it didn't.

I call the Crashlytics methods from a test page in the app so the CrossFirebase.Initialize() will have been called.

Here's the relevant content from my csproj

<!-- https://github.com/TobiasBuchholz/Plugin.Firebase/issues/372 that should no longer be needed with newer dotnet
	but still wasn't working with 9.0.200 -->
	<Target Name="SelectStaticRegistrar" AfterTargets="SelectRegistrar">
		<PropertyGroup Condition="'$(Registrar)' == 'managed-static'">
			<Registrar>static</Registrar>
		</PropertyGroup>
	</Target>

	<ItemGroup>
		<PackageReference Include="Plugin.Firebase" Version="3.1.3" />
	</ItemGroup>

	<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">
		<!--https://github.com/xamarin/GoogleApisForiOSComponents/issues/643#issuecomment-1920970044-->
		<_ExportSymbolsExplicitly>false</_ExportSymbolsExplicitly> 
	</PropertyGroup>
	<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">
		<BundleResource Include="Platforms\iOS\GoogleService-Info.plist" LogicalName="GoogleService-Info.plist"/>
	</ItemGroup>

	<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">
		<GoogleServicesJson Include="Platforms\Android\google-services.json" LogicalName="google-services.json" />
	</ItemGroup>

My init code:

private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
    {
        builder.ConfigureLifecycleEvents(events => {
#if IOS
            events.AddiOS(iOS => iOS.WillFinishLaunching((_,__) => {
                Console.WriteLine("Registering Firebase for iOS");
                CrossFirebase.Initialize();
                return false;
            }));
#elif ANDROID
            events.AddAndroid(android => android.OnCreate((activity, _) => {
                Console.WriteLine("Registering Firebase for Android");
                CrossFirebase.Initialize(activity);
                
            }));
#endif
        });
        
        builder.Services.AddSingleton(_ => CrossFirebaseAuth.Current);
        return builder;
    }

The specific methods:

Plugin.Firebase.Analytics.CrossFirebaseAnalytics.Current.LogEvent("Page OnAppearing"); //This is ok

//Both fail
Plugin.Firebase.Crashlytics.CrossFirebaseCrashlytics.Current.Log("Page OnAppearing");
Plugin.Firebase.Crashlytics.CrossFirebaseCrashlytics.Current.DidCrashOnPreviousExecution();

Any help would gratefully be received!

@AdamEssenmacher
Copy link
Collaborator

Just looking at the plugin code, it looks like you'd experience this if the first call to CrossFirebaseCrashlytics.Current happens before FirebaseCrashlytics.SharedInstance is set:

_instance = FirebaseCrashlytics.SharedInstance;

Would you be able to share a minimal repro project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants