1

I'm trying to implement some sort of security, for In-App Purchases.
I'm trying to implement a method called checkPurchases, which will use the billingClient.queryPurchases()
However when i try to do it, i get a Null Pointer Exception, and Run Time Exception. I do understand what a Null Pointer Exception is, but my issue is that I'm testing the application on a test device, which already "bought" an In App Product, which should be put into the purchasesList. So i don't understand why the List would be empty.

RuntimeException:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{studios.kundby.skatmomsberegner/studios.kundby.skatmomsberegner.InAppBilling}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3108)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3251)
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7045)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)`<br><br>
NullPointer Exception:<br>
`Caused by java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
       at studios.kundby.skatmomsberegner.InAppBilling.checkPurchases(InAppBilling.java:199)
       at studios.kundby.skatmomsberegner.InAppBilling.onCreate(InAppBilling.java:106)
       at android.app.Activity.performCreate(Activity.java:7327)
       at android.app.Activity.performCreate(Activity.java:7318)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3088)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3251)
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7045)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)


This is my checkPurchases method:

private void checkPurchases(){
        Purchase.PurchasesResult result = mBillingClient.queryPurchases(BillingClient.SkuType.INAPP);
        if(result.getPurchasesList().size() >= 0){
            if(result.getPurchasesList().contains(ITEM_SKU_ADREMOVAL)) {
                Log.d(TAG, "ADREMOVAL FUNDET I CHECK PURCHASES");
                mEditor.putBoolean((getResources().getString(R.string.remove_ads_key)), true);
                mEditor.commit();
                }
            }
            else if (!result.getPurchasesList().contains(ITEM_SKU_ADREMOVAL)){
                Log.d(TAG, "ADREMOVAL IKKE FUNDET I CHECK PURCHASES");
                mEditor.putBoolean((getResources().getString(R.string.remove_ads_key)), false);
                mEditor.commit();
            }
        }


I'd like to check if the List contains ITEM_SKU_ADREMOVAL, and if it does, add Boolean to sharedPreferences, which later will handle how to react(This works perfectly)

1 Answers1

1

In your case this line:

Purchase.PurchasesResult result = mBillingClient.queryPurchases(BillingClient.SkuType.INAPP);

returns an object where result.getPurchasesList() == null, which causes the NPE in line

if(result.getPurchasesList().size() >= 0)

because you are invoking .size() on null object.

Piotr S
  • 137
  • 3
  • 2
    That i do understand. But my issue is that i test this on a device, which already bought an In-App on a test account, so in my mind, there should be an item in the list. – Anders Pedersen May 03 '19 at 20:11
  • 1
    Were you able to solve this @AndersPedersen? I am in the exact same situation. – Sagar Jun 26 '19 at 15:41
  • 1
    @Sagar I solved it by wrapping my getPurchasesList in a if statement, checking if it's not null: if (result.getPurchasesList() != null){ if (result.getPurchasesList().size() > 0 ){ – Anders Pedersen Jul 08 '19 at 13:27
  • 2
    @AndersPedersen I have the exact same problem. Did you find out why even if I purchased the item getPurchasesList returns null? – MattButtMatt Apr 22 '20 at 13:03