Java – Android in-app billing crashes when retrieving item prices

Android in-app billing crashes when retrieving item prices… here is a solution to the problem.

Android in-app billing crashes when retrieving item prices

I’m trying to implement Android in-app billing.

What I’ve done so far :

  1. Added AIDl file.
  2. Added util-helper file.
  3. Create an app in Play Console
  4. Upload a signed APK and add the product to Play Console.
  5. Added some code in the Shop.java file. IT WORKED FINE UNTIL I TRIED TO RETRIEVE THE ITEM PRICE (SEE CODE: THIS IS WHERE THE APP CRASHES under).

This is the code I used in onCreate() in Shop.java

onCreate() {

In App Billing
    key = "XXX";
    mHelper = new IabHelper(this, key);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {

if (!result.isSuccess()) {
                Log.d(TAG, "In-app Billing setup failed: " + result);
                return;
            }

if (mHelper == null) return;

Log.d(TAG, "Setup successful. Querying inventory.");
            try {
                mHelper.queryInventoryAsync(mGotInventoryListener);
            } catch (IabHelper.IabAsyncInProgressException e) {

}

}
    });
}

Then there’s mGotInventoryListener:

IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {

 Have we been disposed of in the meantime? If so, quit.
        if (mHelper == null) return;

if (result.isFailure()) {
             handle error
            return;
        }

Log.d(TAG, "Query inventory was successful.");

 Do we have the premium upgrade?
        Purchase premiumPurchase = inventory.getPurchase(ITEM_SKU);
        boolean mIsPremium = (premiumPurchase != null);
        Log.d(TAG, "User is " + (mIsPremium ? " PREMIUM" : "NOT PREMIUM"));

THIS IS WHERE THE APP CRASHES
        String price = inventory.getSkuDetails(ITEM_SKU).getPrice();
        THIS IS WHERE THE APP CRASHES

updateUI();
    }
};

Error message:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: example.com.app, PID: 4270
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String example.com.app.util.SkuDetails.getPrice()' on a  null object reference
                                                                               at example.com.app.Shop$2.onQueryInventoryFinished(Shop.java:234)
                                                                               at example.com.app.util.IabHelper$2$1.run(IabHelper.java:711)
                                                                               at android.os.Handler.handleCallback(Handler.java:751)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

Solution

inventory.getSkuDetails(ITEM_SKU);

Returns the SkuDetails object. But this object can be empty

SkuDetails details = inventory.getSkuDetails(ITEM_SKU);
if (details == null) {
   return;
};
String price = details.getPrice();

Related Problems and Solutions