0

I'm getting an Null Pointer Exception Error when calling the following method from In App Purchase. I tested on my phone and it seemed to work fine. However, on the ANRs & Crashes, I see people are getting the null pointer exception on this line

inv = queryInventory(querySkuDetails, moreItemSkus, moreSubsSkus);

Following is the code from the IAP sample code that I use in my app.

public void queryInventoryAsync(final boolean querySkuDetails, final List<String> moreItemSkus,
        final List<String> moreSubsSkus, final QueryInventoryFinishedListener listener)
    throws IabAsyncInProgressException {
    final Handler handler = new Handler();
    checkNotDisposed();
    checkSetupDone("queryInventory");
    flagStartAsync("refresh inventory");
    (new Thread(new Runnable() {
        public void run() {
            IabResult result = new IabResult(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
            Inventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreItemSkus, moreSubsSkus); // line where null pointer exception is return
            }
            catch (IabException ex) {
                result = ex.getResult();
            }

            flagEndAsync();

            final IabResult result_f = result;
            final Inventory inv_f = inv;
            if (!mDisposed && listener != null) {
                handler.post(new Runnable() {
                    public void run() {
                        listener.onQueryInventoryFinished(result_f, inv_f);
                    }
                });
            }
        }
    })).start();
}

Here's the complete error that I'm seeing from ANRs & crashes

java.lang.NullPointerException: 
  at com.toksampleapp.util.IabHelper.queryPurchases 
   (IabHelper.java:945)
 at com.toksampleapp.util.IabHelper.queryInventory 
(IabHelper.java:624)
  at com.toksampleapp.util.IabHelper$2.run (IabHelper.java:698)
  at java.lang.Thread.run (Thread.java:818)
Julia
  • 1,207
  • 4
  • 29
  • 47
  • Please post complete stack trace from the logcat. – greenapps Dec 12 '17 at 14:41
  • I posted the error that I see from ANRs & crashes. – Julia Dec 12 '17 at 14:47
  • `null pointer exception on this line inv = queryInventory(querySkuDetails, moreItemSkus, moreSubsSkus);` ? I do not see that line in your 'complete' error. You did not post the full stacktrace i think. What is on line (IabHelper.java:945)? Please find out which of your variables is null. `pointer==null`. – greenapps Dec 12 '17 at 14:59
  • Yah, it is actually the complete trace which I thought was weird too because it has only 3 lines. The line 945 is Bundle ownedItems = mService.getPurchases(3, mContext.getPackageName(), itemType, continueToken); – Julia Dec 12 '17 at 15:05
  • line 624 is int r = queryPurchases(inv, ITEM_TYPE_INAPP); – Julia Dec 12 '17 at 15:06
  • Then `mService==null` or `mContext==null`. Check before use and dont use if null. Also check those other variables. – greenapps Dec 12 '17 at 15:23
  • thank you, I think that would fix the issue. – Julia Dec 12 '17 at 15:57

0 Answers0