React-native-iap: buySubscription fails with a null pointer exception on Android

Created on 2 Sep 2018  ·  9Comments  ·  Source: dooboolab/react-native-iap

Version of react-native-iap

react-native-iap: 1.4.0
react-native: 0.45.1

Platforms you faced the error (IOS or Android or both?)

Android only (iOS works perfectly)

Expected behavior

After succesful initialisation and loading of subscriptions:

RNIap.prepare()
RNIap.getSubscriptions(itemSkus)

Buying a subscription should perform a transaction in play store:
RNIap.buySubscription(sku)

Actual behavior

Buying a subscription fails with a runtime exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'int com.android.billingclient.api.BillingClient.launchBillingFlow(android.app.Activity, $
  om.android.billingclient.api.BillingFlowParams)' on a null object reference                                                                                                                                 
       at com.dooboolab.RNIap.RNIapModule.buyItemByType(RNIapModule.java:332)                                                                              
       at java.lang.reflect.Method.invoke(Native Method)                                                                                                   
       at com.facebook.react.cxxbridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)                                                                
       at com.facebook.react.cxxbridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:162)                                                                
       at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)                                                                                
       at android.os.Handler.handleCallback(Handler.java:739)                                                                                              
       at android.os.Handler.dispatchMessage(Handler.java:95)                                                                                              
       at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)                                     
       at android.os.Looper.loop(Looper.java:158)                                                                                                          
       at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:199)                                                    
       at java.lang.Thread.run(Thread.java:818)                                                                                                            

Tested environment (Emulator? Real Device?)

  • Emulator
  • real device (react-native run-android)
  • real device (signed apk)

Steps to reproduce the behavior

In Android, try doing a subscription purchase flow

RNIap.prepare()
RNIap.getSubscriptions(itemSkus)
RNIap.buySubscription(sku)
🙏 help wanted 🤖 android

Most helpful comment

@dooboolab good catch!

The component where I initially set up IAP was unmounting and closing the connection.
Now it works!

Thanks a lot for your help!

All 9 comments

@marioot You may have to wait few hours if you've just added your IAP items to play store. Did you tried api after few hours like (3~6 hours)?

Thanks for the fast response!

It has been more than 24h and also I get them with getSubsriptions. This
happens when trying to purchase.

On Sun, 2 Sep 2018 at 13:00, Hyo Chan Jang notifications@github.com wrote:

@marioot https://github.com/marioot You may have to wait few hours if
you've just added your IAP items to play store. Did you tried api after few
hours like (3~6 hours)?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/dooboolab/react-native-iap/issues/248#issuecomment-417921864,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AEoCLhdSHI4pyqPcJFM40v98VUWoj0iMks5uW7pGgaJpZM4WWgor
.

@marioot Sharing some of your codes may be helpful.

Excuse the debugging console logs :)

const itemSkus = Platform.select({
  ios: [
    YEARLY_SUBSCRIPTION_IOS,
    MONTHLY_SUBSCRIPTION_IOS
  ],
  android: [
    YEARLY_SUBSCRIPTION_ANDROID,
    MONTHLY_SUBSCRIPTION_ANDROID
  ]
});

export const getIapProducts = () => dispatch => {
  RNIap.prepare()
    .then((result) => {
      console.log('Prepare result: ', result);
      RNIap.getSubscriptions(itemSkus)
        .then((products) => {
          console.log(products);
          dispatch({
            type: 'PRODUCTS_REQUEST_SUCCESS',
            products,
          });
        })
        .catch((error) => dispatch({
          type: 'PRODUCTS_REQUEST_FAIL',
          error,
        }));
    })
    .catch((error) => console.log('Prepare error', error));
};

export const purchaseSubscription = (sku) => dispatch => {
  console.log(`purchaseSubscription: ${sku}`);
  dispatch({
    type: 'PURCHASE_TRIGGER'
  });

  return RNIap.buySubscription(sku)   // <----- null pointer
    .then((purchase) => {
      console.info(purchase);
      return Api.registerPurchase(purchase)
        .then(data => {
          console.log('registerPurchase response', data);
          RNIap.finishTransaction();
          dispatch({
            type: 'PURCHASE_SUCCESS',
            receipt: purchase.transactionReceipt,
          });
        });
    })
    .catch(({ code, message }) => {
      dispatch({
        type: 'PURCHASE_FAIL',
        code,
        message,
      });
      RNIap.finishTransaction();
    });
};

@marioot Could you try our recent version which is 2.0.0-alpha.*? It's almost the same except some fixes in variable names after function call.

@dooboolab Just did that, version "react-native-iap": "^2.0.0-alpha12", same error. :(

@marioot Feels like mBillingClient is null in your case for whatever reason. I've just published to 2.0.0-alpha13 for addtional error handling.

Did you call endConnection before calling buysubscription?

@dooboolab good catch!

The component where I initially set up IAP was unmounting and closing the connection.
Now it works!

Thanks a lot for your help!

@marioot For, additional information, I've just updated to 2.0.2 release. prepare method is deprecated and use initConnection instead. It is one of wishlist #236

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jvandenaardweg picture jvandenaardweg  ·  4Comments

coldfins picture coldfins  ·  3Comments

iutin picture iutin  ·  4Comments

ramondelmondo picture ramondelmondo  ·  4Comments

Symyon picture Symyon  ·  5Comments