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

[Android] NullReferenceException on pause/resume before init

Discussion in 'Unity IAP' started by Tamdaralei, Feb 10, 2021.

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

    Tamdaralei

    Joined:
    Feb 10, 2021
    Posts:
    3
    We are seeing a handful amount of exceptions in cloud diagnostic on android after update to core package version 2.2.1 and plugin version 2.2.5 and still present at versions 2.2.2 core and 2.2.7 plugin. Exception occurs if user minimizes and restores app before UnityPurchasing.Initialize call was made. Due to delayed initialization it's easy to reproduce in our app

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

    Managed Stack Trace:

    UnityEngine.Purchasing.ReflectionUtils.HasMethodInInterface (System.Object objectToCheck, System.String methodName) (at <00000000000000000000000000000000>:0)
    UnityEngine.Purchasing.GoogleFetchPurchases.OnFetchedPurchaseSuccessful (System.Collections.Generic.List`1[T] purchases, System.Boolean shouldReturnNonAcknowledgedPurchases) (at <00000000000000000000000000000000>:0)
    UnityEngine.Purchasing.GoogleFetchPurchases+<>c__DisplayClass6_0.<FetchPurchases>b__0 (System.Collections.Generic.List`1[T] purchases) (at <00000000000000000000000000000000>:0)
    System.Action`1[T].Invoke (T obj) (at <00000000000000000000000000000000>:0)
    System.Action`1[T].Invoke (T obj) (at <00000000000000000000000000000000>:0)
    System.Action`1[T].Invoke (T obj) (at <00000000000000000000000000000000>:0)
    System.Action`1[T].Invoke (T obj) (at <00000000000000000000000000000000>:0)
    UnityEngine.Purchasing.Extension.UnityUtil.OnApplicationPause (System.Boolean paused) (at <00000000000000000000000000000000>:0)
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    @Tamdaralei I am not able to reproduce your issue, I tested with the Sample IAP Project here and changed the Toggle button to Initialize and minimized and restored the app before initializing IAP. I'm using IAP 2.2.7. I suspect you have an object going out of scope. Are you using Codeless or Scripted IAP? Do you have any code in Awake()? https://forum.unity.com/threads/sample-iap-project.529555/
     
  3. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    We can reproduce this too. Use switch the app to background before UnityPurchasing.Initialize has been made. It looks like Unity wants to call an interface method which is not yet set (as the app was paused before unity was completely initialised). Unity 2019.4.19f1, newest inapp package (store and service)
     
    Tamdaralei likes this.
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    I am not able to reproduce. Describe exactly how you are switching to background and back to foreground.
     
  5. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    1. Start the app and wait until the launch screen appears
    2. Swipe up from the bottom to put the app in background (keep it in background for 10 seconds or so)
    3. Bring it to foreground again
    4. The exception is logged
    5. We start initializing Unity IAP

    I think that the OnApplicationPause handler from Unity IAP tries to call an interface method of the class object set when initializing unity with the builder. But this didn't happen yet at the time yet.

    We experience a lot of this logs in unity analytics and are pretty sure this happens while facebook initializes and shows the login window and thus switches the app to background. (which happens before unity IAP is initialized)

    Code (CSharp):
    1. UnityEngine.Purchasing.ReflectionUtils.HasMethodInInterface (System.Object objectToCheck, System.String methodName) (at <00000000000000000000000000000000>:0)
    2. UnityEngine.Purchasing.GoogleFetchPurchases.OnFetchedPurchase (System.Collections.Generic.List`1[T] purchases) (at <00000000000000000000000000000000>:0)
    3. System.Action`1[T].Invoke (T obj) (at <00000000000000000000000000000000>:0)
    4. System.Action`1[T].Invoke (T obj) (at <00000000000000000000000000000000>:0)
    5. System.Action`1[T].Invoke (T obj) (at <00000000000000000000000000000000>:0)
    6. UnityEngine.Purchasing.GooglePlayStoreService.DequeueFetchPurchases () (at <00000000000000000000000000000000>:0)
    7. System.Action.Invoke () (at <00000000000000000000000000000000>:0)
    8. 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)
    9. UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, System.Object[] args) (at <00000000000000000000000000000000>:0)
    10. UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod (UnityEngine.AndroidJavaProxy proxy, System.IntPtr jmethodName, System.IntPtr jargs) (at <00000000000000000000000000000000>:0)
    11. Rethrow as TargetInvocationException: UnityEngine.Purchasing.BillingClientStateListener.onBillingSetupFinished(UnityEngine.AndroidJavaObject)
    12. UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, System.Object[] args) (at <00000000000000000000000000000000>:0)
    13. UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod (UnityEngine.AndroidJavaProxy proxy, System.IntPtr jmethodName, System.IntPtr jargs) (at <00000000000000000000000000000000>:0)
    Happens on various Android version from 5.0 up to 10.x
     
    andrew_pearce_ and Tamdaralei like this.
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
  7. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    No, its not codeless, everything is done manually (like we do for years already).

    But basically, I think it's rather simple to reproduce and not dependent on any Unity IAP usage code. The exception happens before we even call a single Unity IAP function.

    As said, it looks like that when Unity resumes, Unity IAP calls a IStoreListener function on the class provided by UnityPurchasing.Initialize(). But this has not been called yet. (We don't call it in awake but later when the user logged in)

    If your sample code initialises Unity IAP in Awake() already I'm pretty sure you cannot reproduce this. Just don't initialise it and make sure Unity IAP calls UnityEngine.Purchasing.GoogleFetchPurchases.OnFetchedPurchase internally when the app is paused/resume. (not sure when this is called, maybe when purchases have been made already earlier)

    We made a 1% staged rollout to 10.000 new users and receive this exception on approx. 7.000 new users (the unique situation with new users is that the app switches to Facebook for the login process, thus pausing the app. When resuming, BEFORE Unity IAP is initialised from us, Unity IAP tries to call a IStoreListener interface function on a null object (because it has not been set yet)

    If you take a look into the code of UnityEngine.Purchasing.ReflectionUtils.HasMethodInInterface I'm pretty sure it tries to call a method on the class which will be set LATER with UnityPurchasing.Initialize() and doesn't check if it is null
     
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    I have a button that Initializes IAP after I background and foreground the app. I understand the issue and your instructions. Regardless it does not reproduce for me. Please compare to the Sample IAP Project which I modified to initialize on a button click. There is no action we can take at this time until we can reproduce unfortunately.
     
  9. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    No problem at all, but as said, even without any single Unity IAP code in our app (only the package and library added, no custom code), this happens. Just see this as a heads up, as its no crash we can simply ignore this for the time being
     
  10. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    It only happens in some instances apparently, but not for the Sample IAP Project. There is something different in your project.
     
  11. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    I'm pretty sure others will report this too in near future, so I will just wait for a fix ;)
     
  12. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    Just a last appendix which might help hunting this down, this didn't happen with any Unity InApp version up to including core 2.2.1 and in-app 2.2.5
     
  13. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    If you want it fixed, I would encourage you to follow my advice. The sooner we can reproduce, the sooner we can address it. I understand now this is a lower priority for you.
     
  14. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    No, its fine, we just simply hoped to take a quick look into the code and see, yeah, there is the possibility that we call an interface method on the not yet store IStoreListener. (there is no need to work with the sample project as it happens with zero code too). Just thought Unity is interested in getting valuable detailed reports...
     
  15. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Do you have such a detailed report? If this is indeed an issue, we would be most interested. You can't get more basic than the Sample IAP Project. We need steps to reproduce (so I can reproduce, not just you) so we can test any potential fix to ensure that it addresses the issue.
     
  16. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    But, we made it even simpler.

    Fresh Unity 2019.4.19f1 project, add in-app package, enable in-app in service window. Build, Download,Pause, Resume -> Exception logged (not a single line of Unity IAP code from our side). So, this is the reason I'm not sure what the Sample IAP Project should change, and even if it works, a plain empty project with 2.2.7 does not...

    If we ever have a bit of spare time I will download the sample IAP project and send the log, but this could take a while...

    Our post here was really just meant to make you aware of an issue (we are not the only ones having this), not to get it fixed for us quickly...
     
  17. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    This is new (and good) information, apparently reproduces by just having IAP installed. We are reviewing the code. I'm performing the same steps but not seeing the issue. That does not mean the issue does not exist, only that I'm not able to reproduce. My suspicion is Android version or something else that we are missing. I'm testing on a Samsung Galaxy S7 Edge Android 8
     
  18. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    Tamdaralei likes this.
  19. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Thanks for the info but not helpful unfortunately, anything else you can think of? I tested on my Samsung Tablet and did not see the issue either.
     
  20. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    The IAP team also looked at this and cannot reproduce. @robertsze Can you remove the /Library folder and zip up the empty project with only IAP installed that reproduces for you and send it to me?
     
  21. John_Corbett

    John_Corbett

    Unity Technologies

    Joined:
    May 17, 2019
    Posts:
    151
    Hello @robertsze and @Tamdaralei,

    We are seeing one way this could possible be reproduced, but only if multiple ticks pass between the creation of the
    StandarPurchasingModule
    object and the call to
    UnityPurchasing.Initialize
    .

    We will look into putting an initialization check into GooglePlayStore to prevent this, but in the meantime, is it possible to revisit your implementation and see if there are multiple ticks between those 2 calls? If you can avoid this, you may prevent the problem from happening in your app.
     
    Tamdaralei likes this.
  22. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    A previous claim states that this occurs with NO IAP code added at all, and only installing IAP can cause the issue. So not related to code ordering or timing. I believe there is something else going on here, an example project would help.
     
  23. Tamdaralei

    Tamdaralei

    Joined:
    Feb 10, 2021
    Posts:
    3
    Sorry for being silent in this thread, issue seems to be unharmful for our users therefore has low priority. Anyway described case was indeed happening in our app - StandarPurchasingModule was created almost at startup and UnityPurchasing.Initialize call was made like 5-8 seconds later. Issue is resolved after some code rearragement, atleast on my device. We will deploy this fix soon and i will post an update. Thanks everyone for helping with this issue
     
    JeffDUnity3D likes this.
  24. John_Corbett

    John_Corbett

    Unity Technologies

    Joined:
    May 17, 2019
    Posts:
    151
  25. Tamdaralei

    Tamdaralei

    Joined:
    Feb 10, 2021
    Posts:
    3
    In our new release this error is completely gone, thanks everyone once again
     
Thread Status:
Not open for further replies.