Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved NullReferenceException in MetricizedGooglePlayStoreService.DequeueQueryProducts

Discussion in 'Unity IAP' started by saltd, Aug 2, 2022.

Thread Status:
Not open for further replies.
  1. livingtarget

    livingtarget

    Joined:
    Apr 21, 2015
    Posts:
    57
    Here's another stacktrace that goes deeper into
    DequeueQueryProducts()

    Code (CSharp):
    1. IndexOutOfRangeException: Index was outside the bounds of the array.
    2.  
    3. Managed Stack Trace:
    4.  
    5. System.Collections.Generic.List`1[T].Add (T item) (at <00000000000000000000000000000000>:0)
    6. Unity.Services.Core.Telemetry.Internal.MetricsPayload.Unity.Services.Core.Telemetry.Internal.ITelemetryPayload.Add (Unity.Services.Core.Telemetry.Internal.ITelemetryEvent telemetryEvent) (at <00000000000000000000000000000000>:0)
    7. Unity.Services.Core.Telemetry.Internal.CacheExtensions.Add[TPayload] (Unity.Services.Core.Telemetry.Internal.CachedPayload`1[TPayload] self, Unity.Services.Core.Telemetry.Internal.ITelemetryEvent telemetryEvent) (at <00000000000000000000000000000000>:0)
    8. Unity.Services.Core.Telemetry.Internal.TelemetryHandler`2[TPayload,TEvent].Register (TEvent telemetryEvent) (at <00000000000000000000000000000000>:0)
    9. Unity.Services.Core.Telemetry.Internal.Metrics.Unity.Services.Core.Telemetry.Internal.IMetrics.SendHistogramMetric (System.String name, System.Double time, System.Collections.Generic.IDictionary`2[TKey,TValue] tags) (at <00000000000000000000000000000000>:0)
    10. UnityEngine.Purchasing.Telemetry.TelemetryMetricsInstanceWrapper.SendMetricByType (UnityEngine.Purchasing.Telemetry.TelemetryMetricParams metricParams) (at <00000000000000000000000000000000>:0)
    11. UnityEngine.Purchasing.Telemetry.TelemetryMetricsInstanceWrapper.SendMetric (UnityEngine.Purchasing.Telemetry.TelemetryMetricTypes metricType, System.String metricName, System.Double metricTimeSeconds) (at <00000000000000000000000000000000>:0)
    12. UnityEngine.Purchasing.Telemetry.TelemetryMetricEvent.StopAndSendMetric () (at <00000000000000000000000000000000>:0)
    13. UnityEngine.Purchasing.MetricizedGooglePlayStoreService.DequeueQueryProducts () (at <00000000000000000000000000000000>:0)
    14. UnityEngine.Purchasing.GooglePlayStoreService.OnDisconnected () (at <00000000000000000000000000000000>:0)
    15. System.Action.Invoke () (at <00000000000000000000000000000000>:0)
    16. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <00000000000000000000000000000000>:0)
    17. System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <00000000000000000000000000000000>:0)
    18. UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, System.Object[] args) (at <00000000000000000000000000000000>:0)
    19. UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod (UnityEngine.AndroidJavaProxy proxy, System.IntPtr jmethodName, System.IntPtr jargs) (at <00000000000000000000000000000000>:0)
    20. Rethrow as TargetInvocationException: UnityEngine.Purchasing.BillingClientStateListener.onBillingServiceDisconnected()
    21. UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, System.Object[] args) (at <00000000000000000000000000000000>:0)
    22. UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod (UnityEngine.AndroidJavaProxy proxy, System.IntPtr jmethodName, System.IntPtr jargs) (at <00000000000000000000000000000000>:0)
     
  2. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    218
    Thanks for all the reports on this issue.
    We have managed to reproduce and solve the NullReferenceException internally. A new release will be out soon with this fix.

    For the IndexOutOfRangeException in Services.Core.Telemetry, this would be another issue, but it might be resolved by updating Services Core to 1.5.2. If this doesn't solve the issue, could you make a new thread, with reproduction steps if possible, and we will investigate it?
     
    Last edited: Oct 27, 2022
  3. jurjenfutureplay

    jurjenfutureplay

    Joined:
    Mar 28, 2022
    Posts:
    3
    Any ETA on the expected release date?

    com.unity.services.core 1.5.1 also does not seem to be released just yet. Any thread to follow on that?
     
    Last edited: Oct 14, 2022
    epernigo likes this.
  4. Arnaud_Gorain

    Arnaud_Gorain

    Unity Technologies

    Joined:
    Jun 28, 2022
    Posts:
    166
    Hi all,

    The new release will be out for the week of October, 24th.
     
  5. joe_nk

    joe_nk

    Joined:
    Jan 6, 2017
    Posts:
    67
    Chiming in with the same issue. We launched an update with the Purchasing package v4.4.1 and have had this happen for over 6k users in the last 72 hours. After digging into this a little bit, I think I've identified the issue and we're going to launch a patch release with the fix.

    When creating a MetricizedGooglePlayStoreService instance, the base (GooglePlayStoreService) constructor calls InitConnectionWithGooglePlay() before MetricizedGooglePlayStoreService has had a chance to initialise m_TelemetryMetricsService.

    InitConnectionWithGooglePlay() registers an OnConnected and an OnDisconnected callback with m_BillingClientStateListener and then calls StartConnection() on the billing client. I'm assuming then that in the presence of no Google Play Store, that immediately returns a non-successful response code to the m_BillingClientStateListener which in turn triggers the OnDisconnected callback, calling DequeueQueryProducts, where we find m_TelemetryMetricsService to be null, since the constructor for MetricizedGooglePlayStoreService hasn't yet completed.

    My fix is to simply make InitConnectionWithGooglePlay() public, remove the call from the GooglePlayStoreService constructor, and call it manually after creating the MetricizedGooglePlayStoreService.

    I've also removed m_TelemetryDiagnostics from MetricizedGooglePlayStoreService as it seems redundant.

    Attached: Screenshot of the changes to the package.
     

    Attached Files:

  6. GerardInc

    GerardInc

    Joined:
    Jan 25, 2017
    Posts:
    15
    Great work. How do you recommend we apply this patch? I fear that overriding code in the folder just means it gets truncated next time the package manager does an update (or checks for integrity, if that is a thing).
     
  7. jurjenfutureplay

    jurjenfutureplay

    Joined:
    Mar 28, 2022
    Posts:
    3
    You can copy the package to the Packages folder and it changes to an In development package that won't be overriden, when you update you have to delete it first and then update.
     
    MagicMiikka likes this.
  8. Mese

    Mese

    Joined:
    Dec 13, 2015
    Posts:
    37
    Hi!

    I have repro this bug. Or at least one part of it?
    It happened to me when I tried to initialize an app from a device (an old one at that) with no google play services installed.

    If you wonder how that's happening in production IF you are only distributing your app through google play (and therefore, devices must have GPGS)... It happens. You APK (aab) ends up all over the internet after being uploaded to Google Play (just check taptap for example, and you might discover your app is "suddenly and magickly there" and with a landing page too!)

    So.... your app is distributed to phones with no GPGS and you try to initiate GooglePlayServices. Bad things happens.

    Before 4.4.0 I remember the error was not this specific error, but IAPs threw another error and were not usable.

    As not only a workaround, but as a safety net here a piece of code everyone should use to check whenever GPGS is avalaible
    (I don't remember where I got this from but it's not mine, do not credit me for this, insert meme of code sharing on the internet here)

    Code (CSharp):
    1.  public bool IsPlayServicesAvailable()
    2.     {
    3.         const string GoogleApiAvailability_Classname =
    4.             "com.google.android.gms.common.GoogleApiAvailability";
    5.         AndroidJavaClass clazz =
    6.             new AndroidJavaClass(GoogleApiAvailability_Classname);
    7.         AndroidJavaObject obj =
    8.             clazz.CallStatic<AndroidJavaObject>("getInstance");
    9.  
    10.         var androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    11.         var activity = androidJC.GetStatic<AndroidJavaObject>("currentActivity");
    12.  
    13.         int value = obj.Call<int>("isGooglePlayServicesAvailable", activity);
    14.  
    15.         string s = value switch
    16.         {
    17.             0 => "ok",
    18.             1 => "missing",
    19.             2 => "need update",
    20.             3 => "disabled",
    21.             18 => "updating",
    22.             9 => "invalid",
    23.             _ => "not ok"
    24.         };
    25.  
    26.         Trace.Log("IAPs: GPGS is " + s + " (" + value + ")");
    27.         // result codes from https://developers.google.com/android/reference/com/google/android/gms/common/ConnectionResult
    28.  
    29.         // 0 == success
    30.         // 1 == service_missing
    31.         // 2 == update service required
    32.         // 3 == service disabled
    33.         // 18 == service updating
    34.         // 9 == service invalid
    35.         return value == 0;
    36.     }
    37.  
    If this bad boy returns false, you should bail and skip Unity IAP initialization. Add try catch for extra protection (noice)

    Can I suggest to the Unity IAP team to just add the feature to avoid Unity IAP init if it's going to fail for this reason?

    I bet this is not the only way this specific error happens (I mean, the trace log) and 4.5.0 eliminated tons of them, at least on our games. If only the Unity Services and "[ServiceCore]" errors where ok. For now, I just mute the warning of "No Unity Services" (when I'm not using them) to avoid the [ServiceCore] errors (that goes on another thread) and check whenever GPGS is avalaible when Unity IAP 4.5.0

    Hope this helps!
     
    livingtarget and Brown2Fox like this.
  9. SamOYUnity3D

    SamOYUnity3D

    Unity Technologies

    Joined:
    May 12, 2019
    Posts:
    597
    Thanks! This is the same fix we did for 4.5.1. As long as the InitConnectionWithGooglePlay is done after the MetricizedGooglePlayStoreService, this NRE will be fixed.
     
    joe_nk likes this.
  10. hippogames

    hippogames

    Joined:
    Feb 5, 2015
    Posts:
    228
    Hi! Still happens in 4.5.0
    NullReferenceException: Object reference not set to an instance of an object. at UnityEngine.Purchasing.MetricizedGooglePlayStoreService.DequeueQueryProducts
     
  11. moayad-jawaker

    moayad-jawaker

    Joined:
    Mar 4, 2020
    Posts:
    10
    When the release will be ready?
     
  12. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    143
    hippogames and -chris like this.
  13. hippogames

    hippogames

    Joined:
    Feb 5, 2015
    Posts:
    228
  14. hippogames

    hippogames

    Joined:
    Feb 5, 2015
    Posts:
    228
    BTW I was able to update by changing 4.5.0 to 4.5.1 in manifest.json and packages-lock.json.
     
  15. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    143
  16. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    218
    Version 4.5.1 is now available and fixes this issue. An announcement regarding it will follow shortly.

    For the other issue, I recommend updating com.unity.services.core (Services Core) to 1.5.2 which should solve it.
    If you still can't find it, you could try adding it via the registry or by name (2021+).
    If updating doesn't solve the error, I would encourage you to make a new thread with the error's details and we will look into it!
     
    smithmj5 and Brown2Fox like this.
  17. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    143
    Many thanks for the reply, and for the info!

    I was able to find Services Core 1.5.2 in Package Manager, I just had to enable preview packages as it's not a verified release yet for Unity 2020.3.40f1.

    If I still see the error when I release this update, I will create a new thread about it.

    Just to note, I had both errors: the main one with Unity IAP that this thread is about, as well as the error with Services Core that I linked (which looks like it originates from IAP as well). Hoping that the updates to IAP and Services Core address both. :)
     
    Yannick_D likes this.
  18. Bartt-Hypernova

    Bartt-Hypernova

    Joined:
    Feb 24, 2020
    Posts:
    7
    Yessssss... Now it's working good. Thanks.
     
    Yannick_D likes this.
  19. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    143
    Wanted to give an update: my game update has rolled out to over 25,000 players so far and I haven't seen a single instance of the bug noted in this thread now. I'm using IAP v4.5.1.

    I also haven't noticed the other error I was seeing, the one noted here: https://forum.unity.com/threads/nul...ueuequeryproducts.1317276/page-2#post-8510543 since updating to Services Core v1.5.2.

    I have a few rare exceptions happening with Services Core, but I'll wait a week or so to see if they happen more, then will create a new thread about them so the Unity team can look into them.
     
  20. damelin

    damelin

    Joined:
    Jul 3, 2012
    Posts:
    64
    Hi, I have updated our game with Service Core 1.5.2 and there is still an exception thrown:
    (while using Unity Purchasing 4.5.1)

    Code (CSharp):
    1. ArgumentException: An item with the same key has already been added. Key: 57
    2.  
    3. Managed Stack Trace:
    4.  
    5. System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <00000000000000000000000000000000>:0)
    6. System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <00000000000000000000000000000000>:0)
    7. Unity.Services.Core.Scheduler.Internal.ActionScheduler.ScheduleAction (System.Action action, System.Double delaySeconds) (at <00000000000000000000000000000000>:0)
    8. Unity.Services.Core.Telemetry.Internal.TelemetryHandler`2[TPayload,TEvent].ScheduleSendingLoop () (at <00000000000000000000000000000000>:0)
    9. Unity.Services.Core.Telemetry.Internal.TelemetryHandler`2[TPayload,TEvent].Register (TEvent telemetryEvent) (at <00000000000000000000000000000000>:0)
    10. Unity.Services.Core.Telemetry.Internal.Metrics.Unity.Services.Core.Telemetry.Internal.IMetrics.SendHistogramMetric (System.String name, System.Double time, System.Collections.Generic.IDictionary`2[TKey,TValue] tags) (at <00000000000000000000000000000000>:0)
    11. UnityEngine.Purchasing.Telemetry.TelemetryMetricsInstanceWrapper.SendMetricByType (UnityEngine.Purchasing.Telemetry.TelemetryMetricParams metricParams) (at <00000000000000000000000000000000>:0)
    12. UnityEngine.Purchasing.Telemetry.TelemetryMetricsInstanceWrapper.SendMetric (UnityEngine.Purchasing.Telemetry.TelemetryMetricTypes metricType, System.String metricName, System.Double metricTimeSeconds) (at <00000000000000000000000000000000>:0)
    13. UnityEngine.Purchasing.Telemetry.TelemetryMetricEvent.StopAndSendMetric () (at <00000000000000000000000000000000>:0)
    14. UnityEngine.Purchasing.MetricizedGooglePlayStoreService.DequeueQueryProducts () (at <00000000000000000000000000000000>:0)
    15. UnityEngine.Purchasing.GooglePlayStoreService.OnConnected () (at <00000000000000000000000000000000>:0)
    16. System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <00000000000000000000000000000000>:0)
    17. System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <00000000000000000000000000000000>:0)
    18. UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, System.Object[] args) (at <00000000000000000000000000000000>:0)
    19. UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod (UnityEngine.AndroidJavaProxy proxy, System.IntPtr jmethodName, System.IntPtr jargs) (at <00000000000000000000000000000000>:0)
    20. Rethrow as TargetInvocationException: UnityEngine.Purchasing.BillingClientStateListener.onBillingSetupFinished(UnityEngine.AndroidJavaObject)
    21. UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, System.Object[] args) (at <00000000000000000000000000000000>:0)
    22. UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod (UnityEngine.AndroidJavaProxy proxy, System.IntPtr jmethodName, System.IntPtr jargs) (at <00000000000000000000000000000000>:0)
     
  21. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    143
    I'm seeing the same thing. We should make a new thread about it, as it's technically a new error. I actually have that one, and two others (also from Services Core). I'll make a thread in a bit, or you can.
     
    damelin likes this.
  22. Arnaud_Gorain

    Arnaud_Gorain

    Unity Technologies

    Joined:
    Jun 28, 2022
    Posts:
    166
    Thanks for the info, we will follow up in the new thread
     
  23. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    143
    Arnaud_Gorain and damelin like this.
  24. domonyiv

    domonyiv

    Joined:
    Oct 1, 2016
    Posts:
    75
    @agorain @Yannick_Denis @SamOYUnity3D I don't mean to be offensive. Out of curiosity, why does it take a month or more to fix bugs like this? I thought a whole team was working on it, but it seems more and more like it's just one person working part-time and there is no time for testing the releases. :)
     
    Last edited: Nov 4, 2022
  25. Arnaud_Gorain

    Arnaud_Gorain

    Unity Technologies

    Joined:
    Jun 28, 2022
    Posts:
    166
    Hi @domonyiv,
    There is a dedicated IAP team with multiple members working on issues like this but also new features and to extend libraries support.

    Release 4.5.1 contains more than just this fix (see release notes). Also, note that we follow a strict release process to ensure the quality of each release.

    We also have a 4.5.2 coming out this month with even more fixes. Feel free to contact me directly if you have more questions as this thread can be closed.
     
    domonyiv likes this.
  26. Arnaud_Gorain

    Arnaud_Gorain

    Unity Technologies

    Joined:
    Jun 28, 2022
    Posts:
    166
    This thread is now closed. Feel free to reach out via a new thread if you encounter further issue.
    Thanks!
     
Thread Status:
Not open for further replies.