Search Unity

[Closed] Lots of nullref errors when retrieving price after startup

Discussion in 'Unity IAP' started by kathode, Nov 20, 2017.

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

    kathode

    Joined:
    Jan 15, 2013
    Posts:
    63
    Hi,

    I have a game that implements Unity IAP. In my initialization code, I retrieve localized prices for items like so:
    Code (CSharp):
    1. itemCost1.Text = iapClass.RetrievePrice("item1name");
    Here's the RetrievePrice method:
    Code (CSharp):
    1. public string RetrievePrice(string title)
    2. {
    3.         return m_StoreController.products.WithID(title).metadata.localizedPriceString;
    4. }
    I'm getting a ton of nullref errors showing up in performance reporting. The specific instance is when the player changes language. The above code is called on startup. If the player changes languages, it is called again. Not sure if that's necessary, but it's that second call that is generating nullreferenceexceptions. I can't seem to repro it locally. No one has reported a visible bug either, but it's by far my #1 exception report.

    Any ideas about what might be going on?
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    What store are you targeting? Do you have more information on the nullref errors, where are they coming from? Also, are you able to tell what locale/language they are using?
     
  3. kathode

    kathode

    Joined:
    Jan 15, 2013
    Posts:
    63
    Targeting Google Play on Android.

    Not much info on the nullrefs other than it occurs in that RetrievePrice function. Something in that chain is reporting as null. I suppose I can put out a build that breaks that call into individual methods for each component to narrow it down and see which part is failing.

    My language switch shouldn't affect the store. All I do is trigger another fetch of the game's strings from a simple XML. I have that call at the top of my post as part of the method which fetches all the strings. I suppose since I'm not doing anything to the player's device locale, another call to RetrievePrice isn't actually necessary. I was just curious if there might be some obvious place that this call would fail after initialization.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Yes, a language could make a difference. In particular, a comma in the price may be the issue, some languages (like Italian) use a comma as the decimal separator and some libraries have problems with that. Do you know the locale (language) that this is occurring in? Can you provide the exact error text?
     
    Last edited: Nov 21, 2017
  5. kathode

    kathode

    Joined:
    Jan 15, 2013
    Posts:
    63
    Hi, sorry for the delay. I finally pushed a build where the long call I referenced in my original post is now broken up into individual function calls. This allowed me to see that my products are returning as null on this call for some reason.

    RetrievePrice now looks like this:
    Code (CSharp):
    1.     public string RetrievePrice(string title)
    2.     {
    3.         var controller = GetStoreController();
    4.         var allProducts = GetProducts(controller);
    5.         var specificData = GetMetadata(allProducts, title);
    6.         return specificData.localizedPriceString;
    7.     }
    GetStoreController looks like this:
    Code (CSharp):
    1.     private IStoreController GetStoreController()
    2.     {
    3.         return m_StoreController;
    4.     }
    The call to GetProducts then returns a nullReferenceException. Here is that method:
    Code (CSharp):
    1.     private ProductCollection GetProducts(IStoreController controller)
    2.     {
    3.         return controller.products;
    4.     }
    Any idea why controller.products would return null?
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please add Debug.Log statement to your code. Is controller null, or is controller.products null? When does it become null? Ideally, please provide full steps to reproduce on a new IAP project.
     
  7. kathode

    kathode

    Joined:
    Jan 15, 2013
    Posts:
    63
    Thanks, of course I will continue to try and repro. My assumption would be that if the controller was null, GetStoreController() would return a nullref, which is why I split the calls up like that. But I can add a log on controller to be doubly sure.
     
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @kathode Sounds good, also please show the code where you are initializing the controller, and the variable scope.
     
Thread Status:
Not open for further replies.