[2020.3] com.unity3d.player.ReflectionHelper$a.<init> fatal exception

Hi, we are facing the following exception in Android, after reviewing firebase dashboard we’ve found the following stack trace:

Caused by java.lang.NullPointerException: Attempt to invoke virtual method ‘int java.lang.String.hashCode()’ on a null object reference
at com.unity3d.player.ReflectionHelper$a.(:19)
at com.unity3d.player.ReflectionHelper.getMethodID(:2)

Fatal Exception: java.lang.Error: FATAL EXCEPTION [Thread-9123]
Unity version : 2020.3.34f1
Android OS: 12

Thanks in advance

6 Likes

Hi! We also have a ton of crashes with this stack trace:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.unity3d.player.ReflectionHelper$a.<init>
at com.unity3d.player.ReflectionHelper.getMethodID

We updated Unity from 2020.1.17f1 to 2020.3.34f1 but it looks like it had no effect on the issue

2 Likes

Hi! Do you have any updates on this errors?
We are experiencing the same error on our side :frowning:

We also see very similar fatal exception in Crashlytics Dashboard. The amount of crashes is critical, because of this exception. Will be good Unity developers response to this thread.

96% of crash devices has OS: Android 12
4% of crash devices has OS: Android 13

We are using Unity 2020.3.41f1 version.

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.unity3d.player.ReflectionHelper$a.<init>(:19)
at com.unity3d.player.ReflectionHelper.getFieldID(:10)

Hi,
I’m currently adding AdMob to my project and I have the same error sometimes, do you use AdMob too ? maybe it’s related because I did not have this but before.

I have the same issue in 2021.3.13f1. Any updates?

I have InApp purchasing in my game v. 4.4.1. WHen game started I got warning message:
Unity IAP: Unity In-App Purchasing requires Unity Gaming Services to have been initialized before use
I added init code:

const string k_Environment = "production";

void Initialize(Action onSuccess, Action<string> onError)
{
    try
    {
        var options = new InitializationOptions().SetEnvironmentName(k_Environment);

        UnityServices.InitializeAsync(options).ContinueWith(task => onSuccess());
    }
    catch (Exception exception)
    {
        onError(exception.Message);
    }
}

void OnSuccess()
{
    var text = "Congratulations!\nUnity Gaming Services has been successfully initialized.";
    informationText.text = text;
    Debug.Log(text);
}

void OnError(string message)
{
    var text = $"Unity Gaming Services failed to initialize with error: {message}.";
    informationText.text = text;
    Debug.LogError(text);
}

And when I added this code I got this error.

Update: when I changed code to

const string k_Environment = "production";

async void Initialize(Action onSuccess, Action<string> onError)
{
    try
    {
        var options = new InitializationOptions().SetEnvironmentName(k_Environment);

        await UnityServices.InitializeAsync(options);

       OnSuccess();
    }
    catch (Exception exception)
    {
        onError(exception.Message);
    }
}

void OnSuccess()
{
    var text = "Congratulations!\nUnity Gaming Services has been successfully initialized.";
    informationText.text = text;
    Debug.Log(text);
}

void OnError(string message)
{
    var text = $"Unity Gaming Services failed to initialize with error: {message}.";
    informationText.text = text;
    Debug.LogError(text);
}

crash stoped.

Yes we use AdMob in our project.

java.lang.NullPointerException: Attempt to invoke virtual method ‘int java.lang.Object.hashCode()’ on a null object reference
at com.unity3d.player.ReflectionHelper$a.(Unknown Source:11)
at com.unity3d.player.ReflectionHelper.getMethodID(Unknown Source:2)
unity 2019.4.10 I met the same problem,

I also encountered the same problem, but I can’t reproduce it locally. Can anyone provide a stable reproduction step?
Unity version : 2020.3.25f1c1
Android OS: 12+
Target:31

Thanks

I had Unity Mediation packages residual files + admob, I think the plugins was colliding.
After removing the Unity Mediation files all is ok.

Hmm seems I talked too fast, the error is still here, but very rare and I can’t find a way to reproduce it.

OK, find a way to reproduce the bug using AdMob at 100%

    public void CreateAndLoadRewardedAd()
    {
        #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/5224354917";
        #elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/1712485313";
        #else
            string adUnitId = "unexpected_platform";
        #endif

        this.rewardedAd = new RewardedAd(adUnitId);

        this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
        this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
        this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded ad with the request.
        this.rewardedAd.LoadAd(request);
    }

    public async void HandleRewardedAdClosed(object sender, EventArgs args)
    {
        await Task.Delay(2000);
        CreateAndLoadRewardedAd(); // here the crash happen !
    }

Just trying to reload the ad after an async delay makes the app crash.
In case of load failed I added a reload after a delay and the crash happend in these case.

To fix the issue in this case just use something like UnityMainThreadDispatcher/Runtime/UnityMainThreadDispatcher.cs at master · PimDeWitte/UnityMainThreadDispatcher · GitHub.

So changing the above code with:

    public async void HandleRewardedAdClosed(object sender, EventArgs args)
    {
        await Task.Delay(2000);
        UnityMainThreadDispatcher.Instance().Enqueue( CreateAndLoadRewardedAd ); // no crash here
    }

Fixes the bug.

Hope this could help someone :wink:

1 Like

We do not use Admob but we are also getting this error:
Unity 2020.3.30f1

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
  at com.unity3d.player.ReflectionHelper$a.<init>
  at com.unity3d.player.ReflectionHelper.getMethodID
1 Like

Do you call native function after an await ?

No, we are not using Await/Async.

Did you managed to find the problem?

After making the update available on the store I have lot of errors spamming in the Android Vital page, I deactivate the ads and the errors stopped.
Anyone finds a way to correct this error ?
Maybe someone from Unity could read this topic and gives some help ?

As I think the bug is related somehow to AdMob I also posted in the AdMob issue tracker:
https://github.com/googleads/googleads-mobile-unity/issues/2480

So it didn’t worked at the end?

It kind of worked as before the fix I had 100% reproductible bug and after I never reproduce it on my test devices. In production I have still some crash because of this bug (1000 crash on 400 unique users on 15 days on my game with near 10000 daily users).
But this bug has caused my game to reach the crash rate limit on google play store, so I had to disable AdMob for the moment. I’m currently trying to resolve the issue (changing ad mediation partner) to be able te re-activate ads on my game.

1 Like