question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Null Reference Exception on Android & iOS in LoadAdError.GetMessage()

See original GitHub issue

[REQUIRED] Step 1: Describe your environment

  • Unity version: 2021.3.1f1
  • Google Mobile Ads Unity plugin version: GoogleMobileAds-v7.3.0
  • Platform: iOS, Android
  • Platform OS version: Android 12, iOS 15.7.1
  • Any specific devices issue occurs on: iPhone SE, Poco X3 Pro
  • Mediation ad networks used, and their versions: None

[REQUIRED] Step 2: Describe the problem

Steps to reproduce:

Import GoogleMobileAds-v7.3.0 into your project. Subscribe to BannerView.OnAdFailedToLoad and in the subscribed method Debug.Log the result of this method LoadAdError.GetMessage() that is called on OnAdFailedToLoad event argument. Ensure Thread safety. Compile it onto Android & iOS Make conditions so an ad doesn’t load. (turn off your internet connection) Observe the result:

Android: Null Reference Exception in IAdErrorClient inherited class. iOS: Thread 1: EXC_BAD_ACCESS (code=2, address=0x16fc77e80) Application Crash.

Relevant Code:

    _bannerView = new BannerView(AdmobConstants.BannerAdUnitId, AdSize.Banner, AdPosition.Top);
    _bannerView.OnAdFailedToLoad += FailedToLoadHandler;
            private void FailedToLoadHandler(object sender, AdFailedToLoadEventArgs e)
            {
                YourUnityClass.ExecuteFromMainThread(() =>
                {
                    Debug.Log($"{e?.LoadAdError.GetMessage()}");
                });
            }

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
NVentimigliacommented, Dec 6, 2022

Hi @mkprivates thanks for raising awareness, I will look deeper into this issue. That said, here is a work around.

private void FailedToLoadHandler(object sender, AdFailedToLoadEventArgs e)
            {
                string errorMessage = e?.LoadAdError.GetMessage();
                MobileEventsExecutor.ExecuteFromMainThread(() =>
                {
                    Debug.Log(errorMessage);
                });
            }

Explanation is this could be a race condition related to releasing of resources in IOS.

0reactions
NVentimigliacommented, Mar 21, 2023

@Hetagkaty

This error is cause by accessing a native object after it is disposed. This can be solved by making sure the object is accessed in the same frame as it is messaged.

private void FailedToLoadHandler(object sender, AdFailedToLoadEventArgs e)
            {
                string errorMessage = e.LoadAdError.GetMessage();
                // Code executed in this function are executed at a later time.
                // The native object may be cleaned up already, resulting in the null.
                MobileEventsExecutor.ExecuteFromMainThread(() =>
                {
                    Debug.Log(errorMessage);
                });
            }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do I get a Null Pointer Exception when I try to run ...
When I click the button to call the ad, the program crashes and makes reference to a Null Pointer Exception caused by the...
Read more >
NullReferenceException on App.Current.MainPage
that is called from Android native OnMessageReceived() , throws NullReferenceException . Why can this happen? Isn't App.Current to be accessible ...
Read more >
Interstitial ads | Android
To load an interstitial ad, call the InterstitialAd static load() method and pass ... Set the ad reference to null so you don't...
Read more >
Null Reference Exception error occurs when trying to ...
The main thing it's complaining about is that it doesn't have an <application> tag wrapping the activity.
Read more >
FirebaseAuthException - Google
FirebaseAuthMissingActivityForRecaptchaException, Thrown when the auth request attempted to fetch a reCAPTCHA token, but the activity is missing or null.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found