0

I am trying to get PayPal button work at Checkout Activity in my Android App. But I am getting a Null Pointer Exception, It refers to line 1585 in my code. The logcat output is as follows:

03-22 21:27:28.659: W/dalvikvm(419): threadid=1: thread exiting with uncaught exception (group=0x40014760)
03-22 21:27:28.669: E/AndroidRuntime(419): FATAL EXCEPTION: main
03-22 21:27:28.669: E/AndroidRuntime(419): java.lang.NullPointerException
03-22 21:27:28.669: E/AndroidRuntime(419):  at java.math.BigDecimal.<init>(BigDecimal.java:406)
03-22 21:27:28.669: E/AndroidRuntime(419):  at com.fokrul.justdeals.ActivityTab$PaypalTask$1.onClick(ActivityTab.java:1585)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.view.View.performClick(View.java:3110)
03-22 21:27:28.669: E/AndroidRuntime(419):  at com.paypal.android.MEP.CheckoutButton.onClick(Unknown Source)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.view.View.performClick(View.java:3110)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.view.View$PerformClick.run(View.java:11934)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.os.Handler.handleCallback(Handler.java:587)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.os.Looper.loop(Looper.java:132)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.app.ActivityThread.main(ActivityThread.java:4123)
03-22 21:27:28.669: E/AndroidRuntime(419):  at java.lang.reflect.Method.invokeNative(Native Method)
03-22 21:27:28.669: E/AndroidRuntime(419):  at java.lang.reflect.Method.invoke(Method.java:491)
03-22 21:27:28.669: E/AndroidRuntime(419):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-22 21:27:28.669: E/AndroidRuntime(419):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-22 21:27:28.669: E/AndroidRuntime(419):  at dalvik.system.NativeStart.main(Native Method)

My Code for Onclick method for PayPal activity is below:

class PaypalTask extends AsyncTask<Void, Void, Void> {
        protected final char[] TOTAL_GBP = null;

        public Void doInBackground(Void... arg0) {
            ppObject = PayPal.initWithAppID(getBaseContext(), "APP-80W284485P519543T", PayPal.ENV_SANDBOX);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            btnPaypal = ppObject.getCheckoutButton(getApplicationContext(), PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            btnPaypal.setLayoutParams(params);
            rlPay.addView(btnPaypal);
            if(utils.prefs.getBoolean("isLogged", false)) btnPaypal.setVisibility(View.VISIBLE); else btnPaypal.setVisibility(View.GONE);

            btnPaypal.setOnClickListener(new OnClickListener(){
                public void onClick(View view) {

                    // to check whether there are any books in the shopping cart
                    if(!cid.isEmpty()){
                        PayPalPayment payment = new PayPalPayment();
            <<LINE 1585-->>>>> payment.setSubtotal(new BigDecimal(TOTAL_GBP));
                        payment.setCurrencyType("USD");
                        payment.setRecipient(Utils.PAYPAL_ACCOUNT);
                        payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);
                        Intent checkoutIntent = PayPal.getInstance().checkout(payment, getBaseContext());
                        startActivityForResult(checkoutIntent, 1);
                    }else{
                        utils.showToast("Your shopping cart is empty.");
                        // The PayPal button needs to be updated so that the user can click on it again.
                        btnPaypal.updateButton();
                    }
                }   
            });
        }
    }

Any thoughts what might be going wrong here?

1 Answers1

0

The obvious answer is a recap from comments:

protected final char[] TOTAL_GBP = null;

dont set null as a constructor parameter for BigDecimal(TOTAL_GBP) @payment.setSubtotal(new BigDecimal(TOTAL_GBP));

example: payment.setSubtotal(new BigDecimal("8.25")); taken from here http://androiddevelopement.blogspot.com/2011/04/adding-paypal-payment-in-android.html about paypal and android integration.

The last but not least:

You might also want to rethink your AsyncTask process and doInBackground() method and your onPostExecute() you usually don't set onClick() listeners in the AsyncTask. They should rather have to be in your Activity and running in your main thread then within a button click you may call on execution of the AsyncTask.

More on AsyncTasks : http://developer.android.com/reference/android/os/AsyncTask.html AsyncTask Android example


Hope it clarifies the matter a bit.

Cheers

Community
  • 1
  • 1
Sergey Benner
  • 4,421
  • 2
  • 22
  • 29