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)