IAP sometimes returns null on IOS

Here is the error log from Crashlytics. I couldn’t find the reason why it’s returning as null. When I test the game on my phone(iPhone 7 Plus) purchasing giving late response. I clicks the product and waiting 10 seconds then appearing purchase screen. Even sometimes not coming purchase screen, most probably returning null in this times.

Non-fatal Exception: NullReferenceException
0  ???                            0x0 BuySmallGem (IAPManager)
1  ???                            0x0 Invoke (UnityEngine.Events.UnityAction)
2  ???                            0x0 Invoke (UnityEngine.Events.UnityEvent)
3  ???                            0x0 Invoke (UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1])
4  ???                            0x0 Execute[T] (UnityEngine.EventSystems.ExecuteEvents)
5  ???                            0x0 ProcessTouchPress (UnityEngine.EventSystems.StandaloneInputModule)
6  ???                            0x0 ProcessTouchEvents (UnityEngine.EventSystems.StandaloneInputModule)
7  ???                            0x0 Process (UnityEngine.EventSystems.StandaloneInputModule)
8  ???                            0x0 StandaloneInputModule:Process (UnityEngine.EventSystems)

The error log you provided suggests that there is a NullReferenceException occurring in the BuySmallGem method of your IAPManager class, which is likely caused by a null value being returned by the IAP system on iOS.

To address this issue, you should check that the IAP system is properly initialized and that the product you are attempting to purchase is actually available in the IAP catalog. You should also check if the user is connected to the internet, as the IAP system requires an internet connection to function properly.

In addition, you may want to add some error handling code to your BuySmallGem method to catch any null values and prevent the app from crashing. You can use a try-catch block to catch the NullReferenceException and display an error message to the user indicating that the purchase was not successful.

Here’s an example of how you can add error handling to your BuySmallGem method:

public void BuySmallGem()
{
    try
    {
        // Attempt to purchase the small gem product
        var product = m_Controller.products.WithID("small_gem");

        if (product != null && product.availableToPurchase)
        {
            m_Controller.InitiatePurchase(product);
        }
        else
        {
            Debug.Log("The small gem product is not available for purchase.");
        }
    }
    catch (NullReferenceException e)
    {
        // Display an error message to the user if the IAP system returns null
        Debug.Log("Failed to purchase small gem: " + e.Message);
    }
}

By adding error handling code like this, you can prevent the app from crashing and provide a better user experience for your customers.

Let me know how you get on.

Hi,

Thank you for replied for my problem. I applied your code but I didn’t get any error message but I realized something more that I am getting null error like at below while game is starting.

NullReferenceException: Object reference not set to an instance of an object.
  at IAPManager.ProcessPurchase (UnityEngine.Purchasing.PurchaseEventArgs args) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.Purchasing.PurchasingManager.ProcessPurchaseIfNew (UnityEngine.Purchasing.Product product) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.Purchasing.PurchasingManager.ProcessPurchaseOnStart () [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.Purchasing.PurchasingManager.OnProductsRetrieved (System.Collections.Generic.List`1[T] products) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.Purchasing.AppleStoreImpl.OnProductsRetrieved (System.String json) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.Purchasing.Extension.UnityUtil.Update () [0x00000] in <00000000000000000000000000000000>:0

Hello guralp,

Would you be able to share your ProcessPurchase code?
Also, have you tried debugging it to see which object is null?

Hi Yannick,

I don’t know where it is. But most probably this null error coming while initializing. Because I see this error when launching the game.

    void InitializePurchasing()
    {
        var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

        //Add products that will be purchasable and indicate its type.
        builder.AddProduct(specialBundleId, ProductType.NonConsumable);
        builder.AddProduct(offerId, ProductType.Consumable);
        builder.AddProduct(smallGemId, ProductType.Consumable);
        builder.AddProduct(mediumGemId, ProductType.Consumable);
        builder.AddProduct(bigGemId, ProductType.Consumable);

        UnityPurchasing.Initialize(this, builder);
    }