Java – NullPointerException in iabhelper In querypurchases
Today, I found an application crash report for Android applications involving the following stack traces:
java.lang.NullPointerException: Attempt to invoke interface method 'android.os.Bundle com.android.vending.billing.IInAppBillingService.getPurchases(int,java.lang.String,java.lang.String)' on a null object reference at com.myapp.utils.IabHelper.queryPurchases(IabHelper.java:878) at com.myapp.utils.IabHelper.queryInventory(IabHelper.java:572) at com.myapp.utils.IabHelper.queryInventory(IabHelper.java:545) at com.myapp.utils.IabHelper$2.run(IabHelper.java:645) at java.lang.Thread.run(Thread.java:818)
(the line number is from the original – or what looks like to be the original, because it is reformatted by user)
In general, you can modify your code to check for unassigned class members The problem is that this code is copied and pasted from the Android SDK, because iabhelper is a class, and Android SDK providers are a good starting point for implementing in app settlement v3
The guilty is the second
logDebug("Calling getPurchases with continuation token: " + continueToken); Bundle ownedItems = mService.getPurchases(3,mContext.getPackageName(),itemType,continueToken);
It appears that the service was not connected when the method was called This error occurs on the nexus 5 device (according to the developer console)
Is this a known issue with Android 5? > Is there the latest version of IAB assistant? > What can I do instead of manually editing code to handle NPE?
Solution
I modified this code
do { logDebug("Calling getPurchases with continuation token: " + continueToken); Bundle ownedItems = mService.getPurchases(3,continueToken); // ... }
Become this
do { logDebug("Calling getPurchases with continuation token: " + continueToken); if (mService == null || mContext == null) { logError("Our service and/or our context are null. Exiting."); return IABHELPER_UNKNowN_ERROR; } Bundle ownedItems = mService.getPurchases(3,continueToken); // ... }
This is almost certainly because the process runs asynchronously and the activity / application (or being) is closed when the result is returned