Skip to main content
Solved

NullPointerException in PurchaseParams.Builder (Android: java)


Forum|alt.badge.img+4

This is the Crash Report I got from the Play Console: 

Type

java.lang.NullPointerException
Exception java.lang.NullPointerException:
  at com.revenuecat.purchases.PurchaseParams$Builder.<init> (PurchaseParams.kt)
  at com.xxxxxxxxxxxx.MainActivity.purchase (MainActivity.java:2103)
  at com.xxxxxxxxxxxx.MainActivity.lambda$showPaywall$33 (MainActivity.java:2014)
  at android.view.View.performClick (View.java:7792)
  at android.widget.TextView.performClick (TextView.java:16112)
  at android.view.View.performClickInternal (View.java:7769)
  at android.view.View.access$3800 (View.java:910)
  at android.view.View$PerformClick.run (View.java:30218)
  at android.os.Handler.handleCallback (Handler.java:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loopOnce (Looper.java:226)
  at android.os.Looper.loop (Looper.java:313)
  at android.app.ActivityThread.main (ActivityThread.java:8751)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)

Here is my code:

    protected void purchase(Package purchasePackage) {
        Log.d(TAG_REVENUECAT, "RC: Purchases.getSharedInstance().purchase");
        Purchases.getSharedInstance().purchase(
            new PurchaseParams.Builder(this, purchasePackage).build(), new PurchaseCallback() {
                @Override
                public void onCompleted(@NonNull StoreTransaction storeTransaction, @NonNull CustomerInfo customerInfo) {
                    if (customerInfo.getEntitlements().get(PREMIUM_ENTITLEMENT_ID) != null) {
                        if (Objects.requireNonNull(customerInfo.getEntitlements().get(PREMIUM_ENTITLEMENT_ID)).isActive()) {
                            // Unlock that great "pro" content
                            Log.d(TAG_REVENUECAT, "RC: Purchase completed");
                            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity.this);
                            alertBuilder.setTitle(R.string.thankyou)
                                .setCancelable(false)
                                .setPositiveButton(android.R.string.ok, (dialog, id) -> {})
                                .show();
                            enablePremium(true);
                        }
                    }
                }
                @Override
                public void onError(@NonNull PurchasesError purchasesError, boolean userCancelled) {
                    // No purchase
                    Log.e(TAG_REVENUECAT, purchasesError.toString());
                    Log.d(TAG_REVENUECAT, "Purchase error");
                    if (!userCancelled) {
                        showErrorAlert(purchasesError);
                    }
                }
            }
        );
    }
About 1% of users affected. I suspect “storeTransaction” or “customerInfo” having null. 
public void onCompleted(@NonNull StoreTransaction storeTransaction, @NonNull CustomerInfo customerInfo)

Should I check

customerInfo.getEntitlements() != null too?

 

 

 

Best answer by toni-rico

Hi @orangutan,

Looks like this crash may be happening if `purchasePackage` is null when passed to the `PurchaseParams.Builder` constructor, since it’s a not null parameter. Can you confirm whether `purchasePackage` may be null on your code?

Let us know if you need any more help!

View original
Did this post help you find an answer to your question?

3 replies

Forum|alt.badge.img+3
  • RevenueCat Staff
  • 55 replies
  • Answer
  • December 4, 2023

Hi @orangutan,

Looks like this crash may be happening if `purchasePackage` is null when passed to the `PurchaseParams.Builder` constructor, since it’s a not null parameter. Can you confirm whether `purchasePackage` may be null on your code?

Let us know if you need any more help!


Forum|alt.badge.img+4
  • Author
  • New Member
  • 3 replies
  • December 4, 2023
toni-rico wrote:

Hi @orangutan,

Looks like this crash may be happening if `purchasePackage` is null when passed to the `PurchaseParams.Builder` constructor, since it’s a not null parameter. Can you confirm whether `purchasePackage` may be null on your code?

Let us know if you need any more help!

 

Thanks for your prompt reply.

 

You're right, for example, if the network isn't connected, getOfferings gives null AvailablePackages.

I've changed the code to check if purchasePackage is null.

I'll let you know the results in a few days.

 

Thanks!


Forum|alt.badge.img+4
  • Author
  • New Member
  • 3 replies
  • December 7, 2023
orangutan wrote:
toni-rico wrote:

Hi @orangutan,

Looks like this crash may be happening if `purchasePackage` is null when passed to the `PurchaseParams.Builder` constructor, since it’s a not null parameter. Can you confirm whether `purchasePackage` may be null on your code?

Let us know if you need any more help!

 

Thanks for your prompt reply.

 

You're right, for example, if the network isn't connected, getOfferings gives null AvailablePackages.

I've changed the code to check if purchasePackage is null.

I'll let you know the results in a few days.

 

Thanks!

 

Problem solved! Thanks a lot. 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings