google wallet integration null reference android

1.1k Views Asked by At

I am trying to integration google wallet in my app. I have followed this tutorial https://developers.google.com/commerce/wallet/instant-buy/android/tutorial

But i am getting a null reference error at

mWalletClient = new WalletClient(this, WalletConstants.ENVIRONMENT_SANDBOX,"[email protected]" ,
                WalletConstants.THEME_HOLO_LIGHT, connectionCallbackListener , connectionFailedListener); 

due to the connectionFailedListener and connectionCallbackListener function.

This is the following code which i have tried:

  import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
  import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
    import com.google.android.gms.wallet.Address;
    import com.google.android.gms.wallet.Cart;
    import com.google.android.gms.wallet.FullWallet;
    import com.google.android.gms.wallet.FullWalletRequest;
    import com.google.android.gms.wallet.LineItem;
    import com.google.android.gms.wallet.MaskedWallet;
    import com.google.android.gms.wallet.MaskedWalletRequest;
    import com.google.android.gms.wallet.NotifyTransactionStatusRequest;
    import com.google.android.gms.wallet.WalletClient;
    import com.google.android.gms.wallet.WalletConstants;

    public class Upgrade extends Activity {
        static final int REQUEST_CODE_RESOLVE_PRE_AUTH = 1010;
        static final int REQUEST_CODE_RESOLVE_LOAD_MASKED_WALLET = 1001;
        static final int REQUEST_CODE_RESOLVE_CHANGE_MASKED_WALLET = 1002;
        static final int REQUEST_CODE_LOAD_MASKED_WALLET=1000;
        static final int REQUEST_CODE_RESOLVE_LOAD_FULL_WALLET = 1004;
        private static final String googleTransactionId = null;


          MaskedWallet mMaskedWallet;
          FullWallet mFullWallet;
        WalletClient mWalletClient;
        NotifyTransactionStatusRequest notifyTxRequest;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Set View to register.xml
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
            setContentView(R.layout.upgrade);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                    R.layout.profile_custom_titlebar);
            ConnectionCallbacks connectionCallbackListener = null;
            OnConnectionFailedListener connectionFailedListener = null;
            mWalletClient = new WalletClient(this, WalletConstants.ENVIRONMENT_SANDBOX,"[email protected]" ,
                    WalletConstants.THEME_HOLO_LIGHT, connectionCallbackListener , connectionFailedListener);
            Button btnCompletePurchase=(Button)findViewById(R.id.btnCompletePurchase);
            btnCompletePurchase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    completepurchase();
                     notifyTxRequest = NotifyTransactionStatusRequest.newBuilder()
                              .setGoogleTransactionId(mFullWallet.getGoogleTransactionId())
                              .setStatus(NotifyTransactionStatusRequest.Status.SUCCESS)
                              .build();

                }
            });
            displayListView();
        }
        @Override
        public void onStart() {
            super.onStart();
            mWalletClient.connect();
        }

        public void connectionCallbackListener()
        {
            Log.d("success","success");
        }
        public void connectionFailedListener()
        {

            Log.d("error","error");
        }
        public void completepurchase()
        {
            mWalletClient.loadMaskedWallet(maskedWalletRequest,
                    REQUEST_CODE_LOAD_MASKED_WALLET);


            mWalletClient.notifyTransactionStatus(notifyTxRequest);

            mWalletClient.changeMaskedWallet(mFullWallet.getGoogleTransactionId(),mFullWallet.getMerchantTransactionId(),REQUEST_CODE_RESOLVE_CHANGE_MASKED_WALLET);
            mWalletClient.loadFullWallet(fullWalletRequest,REQUEST_CODE_RESOLVE_LOAD_FULL_WALLET);

            String accountNumber  =  mFullWallet.getProxyCard().getPan();
            String securityCvv  = mFullWallet.getProxyCard().getCvn();
            //int expirationYear = mFullWallet.getExpirationYear();
            //int expirationMonth = mFullWallet.getExpirationMonth();
            Address billingAddress = mFullWallet.getBillingAddress();
            Address shippingAddress = mFullWallet.getShippingAddress();







        }



        FullWalletRequest fullWalletRequest = FullWalletRequest.newBuilder()
                  //.setGoogleTransactionId( mMaskedWallet.getGoogleTransactionId())
                .setGoogleTransactionId(googleTransactionId)
                  .setCart(Cart.newBuilder()
                          .setCurrencyCode("USD")
                         .setTotalPrice("10.00")
                          .addLineItem(LineItem.newBuilder()
                                  .setCurrencyCode("USD")
                                  .setDescription("Premium Membership")
                                  .setQuantity("1")
                                 .setUnitPrice("10")
                              .setTotalPrice("10.00")
                                  .build())
                          .build())
                  .build();


        MaskedWalletRequest maskedWalletRequest =
                  MaskedWalletRequest.newBuilder()
                  //.setMerchantName(Constants.MERCHANT_NAME)
                  .setMerchantName("Udebate")
                  .setPhoneNumberRequired(true)
                  .setShippingAddressRequired(true)
                  .setCurrencyCode("USD")
                  .setCart(Cart.newBuilder()
                     // .setCurrencyCode(Constants.CURRENCY_CODE_USD)
                          .setCurrencyCode("USD")
                      .setTotalPrice("10.00")
                      .addLineItem(LineItem.newBuilder()
                              .setCurrencyCode("USD")
                              .setDescription("Premium Membership")
                              .setQuantity("1")
                              .setUnitPrice("10")
                              .setTotalPrice("10.00")
                              .build())

                      .build())
                  .setEstimatedTotalPrice("10.00")
                  .build();

                @Override
                public void onStop() {
                  super.onStop();
                  mWalletClient.disconnect();
                }




        public void onConnected(Bundle connectionHint) {
            mWalletClient.checkForPreAuthorization(
                REQUEST_CODE_RESOLVE_PRE_AUTH);
        }

        public void onActivityResult(int requestCode, int resultCode,
                Intent data) {

            switch (requestCode) {
                case REQUEST_CODE_RESOLVE_PRE_AUTH:
                    switch (resultCode) {
                        case Activity.RESULT_OK:
                            boolean mIsPreAuthed = data.getBooleanExtra(

                                    WalletConstants.EXTRA_IS_USER_PREAUTHORIZED, false);

                            break;
                        case Activity.RESULT_CANCELED:
                            break;
                        case REQUEST_CODE_RESOLVE_LOAD_MASKED_WALLET:
                          switch (resultCode) {
                              case Activity.RESULT_OK:
                                   mMaskedWallet =
                                          data.getParcelableExtra(WalletConstants.EXTRA_MASKED_WALLET);
                                  launchConfirmationPage(mMaskedWallet);
                                  break;
                              case Activity.RESULT_CANCELED:
                                  // The user cancelled the flow
                                  break;
                              default:
                                  // Handle the error
                                  int errorCode =
                                      data.getIntExtra(
                                          WalletConstants.EXTRA_ERROR_CODE, -1);
                                  handleError(errorCode);
                                  break;
                          }
                          break;
                        case REQUEST_CODE_RESOLVE_CHANGE_MASKED_WALLET:
                            switch (resultCode) {
                                case Activity.RESULT_OK:
                                    mMaskedWallet =
                                    data.getParcelableExtra(
                                        WalletConstants.EXTRA_MASKED_WALLET);
                                    // Update UI with new Masked Wallet
                                    break;
                                case Activity.RESULT_CANCELED:
                                    // nothing to do here
                                    break;
                                default:
                                    int errorCode =
                                        data.getIntExtra(
                                            WalletConstants.EXTRA_ERROR_CODE, -1);
                                        handleError(errorCode);
                                        break;
                            }
                            break;
                        case REQUEST_CODE_RESOLVE_LOAD_FULL_WALLET:
                            switch (resultCode) {
                                case Activity.RESULT_OK:
                                    mFullWallet  =
                                    data.getParcelableExtra(
                                        WalletConstants.EXTRA_FULL_WALLET);
                                    // the full wallet can now be used to
                                    // process the customer's payment. Send the
                                    // wallet info up to server to process, and to
                                    // get the result for sending
                                    // transaction status
                                    break;
                                case Activity.RESULT_CANCELED:
                                    // nothing to do here
                                    break;
                                default:
                                    int errorCode =
                                        data.getIntExtra(
                                            WalletConstants.EXTRA_ERROR_CODE, -1);
                                    handleError(errorCode);
                                    break;
                            }
                            break;

                        default:
                            break;
                    }

            }
        }





        private void launchConfirmationPage(MaskedWallet mMaskedWallet2) {
            // TODO Auto-generated method stub
            Log.d("confirmed","confirmed");

        }






        void handleError(int errorCode) {
            switch (errorCode) {
                case WalletConstants.ERROR_CODE_SPENDING_LIMIT_EXCEEDED:
                    Toast.makeText(this,"Spending limit exceeded",
                            Toast.LENGTH_LONG).show();
                    break;
                case WalletConstants.ERROR_CODE_INVALID_PARAMETERS:
                case WalletConstants.ERROR_CODE_AUTHENTICATION_FAILURE:
                case WalletConstants.ERROR_CODE_BUYER_ACCOUNT_ERROR:
                case WalletConstants.ERROR_CODE_MERCHANT_ACCOUNT_ERROR:
                case WalletConstants.ERROR_CODE_SERVICE_UNAVAILABLE:
                case WalletConstants.ERROR_CODE_UNSUPPORTED_API_VERSION:
                case WalletConstants.ERROR_CODE_UNKNOWN:
                default:
                    // unrecoverable error
                   // mGoogleWalletDisabled = true;
                   // displayGoogleWalletErrorToast(errorCode);
                    break;
            }
        }

    }

This is my logcat :

12-05 06:01:37.437: E/AndroidRuntime(1660): FATAL EXCEPTION: main
12-05 06:01:37.437: E/AndroidRuntime(1660): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.main.udebate/com.main.udebate.Upgrade}: java.lang.NullPointerException: null reference
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.os.Looper.loop(Looper.java:137)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at java.lang.reflect.Method.invoke(Method.java:511)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at dalvik.system.NativeStart.main(Native Method)
12-05 06:01:37.437: E/AndroidRuntime(1660): Caused by: java.lang.NullPointerException: null reference
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.google.android.gms.internal.dm.e(Unknown Source)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.google.android.gms.internal.de.<init>(Unknown Source)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.google.android.gms.internal.gj.<init>(Unknown Source)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.google.android.gms.wallet.WalletClient.<init>(Unknown Source)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.main.udebate.Upgrade.onCreate(Upgrade.java:56)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.Activity.performCreate(Activity.java:5104)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-05 06:01:37.437: E/AndroidRuntime(1660):     ... 11 more
3

There are 3 best solutions below

0
On

I must say I'm not familar with wallet at all. But I think it is your problem that you put null values into that function:

ConnectionCallbacks connectionCallbackListener = null;
OnConnectionFailedListener connectionFailedListener = null;
mWalletClient = new WalletClient(this,
                                 WalletConstants.ENVIRONMENT_SANDBOX,
                                 "[email protected]",
                                 WalletConstants.THEME_HOLO_LIGHT,
                                 connectionCallbackListener, // <-- this is null
                                 connectionFailedListener);  // <-- this is null

I think you have just to implement a real listener.

0
On

You'll need to implement both GooglePlayServicesClient.ConnectionCallbacks and GooglePlayServicesClient.OnConnectionFailedListener. Ideally you can have the current activity implement the two interfaces and pass "this" into the WalletClient constructor.

Note that there is a sample application included in the SDK manager that can be found at <android-sdk>/extras/google/google_play_services/samples/wallet.

Hope that helps

Nagesh

0
On

I faced the same problem but if I do the following the NPE is gone Let your class implement OnConnectionFailedListener, ConnectionCallbacks and then

mWalletClient = new WalletClient(this, WalletConstants.ENVIRONMENT_SANDBOX, account_name, WalletConstants.THEME_HOLO_LIGHT, this, this);

hope this helps