Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Convert string to bigdecimal in android

Hi How can i convert string to bigdecimal in android.

This is my 1st activity:

public class ViewCartActivity extends Activity {

String mGrandTotal;
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    ListView mLstView1 = (ListView) findViewById(R.id.listView1);
    TextView mTxtViewGrandTotal = (TextView) findViewById(R.id.mTxtViewGrandTotalValue);
    Button mBtnSubmit = (Button) findViewById(R.id.mBtnSubmit);

    ViewCartAdapter mViewCartAdpt = new ViewCartAdapter(


    if (Constants.mItem_Detail.size() > 0) {
        Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0)
        for (int i = 1; i < Constants.mItem_Detail.size(); i++) {
            mGTotal = mGTotal
                    + Double.parseDouble(Constants.mItem_Detail.get(i).get(

        mGrandTotal = String.valueOf(mGTotal);
        mTxtViewGrandTotal.setText("$" + mGrandTotal);

    mBtnSubmit.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

             Intent i = new Intent(getApplicationContext(), CustomerLogin.class);
            i.putExtra("GrandTotal", mGrandTotal);


The CustomerLogin.java(next activity):

      String mGrandTotal;
      /** Called when the activity is first created. */
       public void onCreate(Bundle savedInstanceState) {
       Bundle b = getIntent().getExtras();

         String total = b.getString("GrandTotal");
           if(isUserValidated && isPasswordValidated)
            String s= getIntent().getStringExtra(mGrandTotal);
                Intent intent = new Intent(CustomerLogin.this,PayPalIntegrationActivity.class);
                intent.putExtra(mGrandTotal, s);



The PayPalIntegrationActivity.java(3rd activity) look like:

public class PayPalIntegrationActivity extends Activity implements OnClickListener {

 String mGrandTotal;
    private PayPal mPayPal;
   private CheckoutButton launchPayPalButton;

    public void onCreate(Bundle savedInstanceState) {


    public void onClick(View v) {


         private PayPalPayment  payWithPaypal() {
                PayPalPayment newPayment = new PayPalPayment();
                Intent in = getIntent();
                String total = in.getStringExtra(mGrandTotal);
                BigDecimal sPrice = new BigDecimal(total);


I have to pass the grandtotal value to my paypalintegration activity.

But my console window shows following error:

      12-21 05:52:09.571: E/AndroidRuntime(782): FATAL EXCEPTION: main
       12-21 05:52:09.571: E/AndroidRuntime(782): java.lang.NullPointerException
       12-21 05:52:09.571: E/AndroidRuntime(782):   at java.math.BigDecimal.<init>(BigDecimal.java:483)
      12-21 05:52:09.571: E/AndroidRuntime(782):    at com.ssmobileproductions.catalogue.PayPalIntegrationActivity.payWithPaypal(PayPalIntegrationActivity.java:74)
        12-21 05:52:09.571: E/AndroidRuntime(782):  at com.ssmobileproductions.catalogue.PayPalIntegrationActivity.onClick(PayPalIntegrationActivity.java:66)
      12-21 05:52:09.571: E/AndroidRuntime(782):    at android.view.View.performClick(View.java:2408)

please help me.whats wrong in my code.

I wish to need the o/p is:

mGrandTotal value is pass from my 1st activity to last activity(paypalintegration activity)

mGrandTotal value is set to below line: newPayment.setSubtotal(sPrice);

How can i do.please let me know

like image 999
user1897014 Avatar asked Feb 19 '23 02:02


1 Answers

You are not getting error because of converting String to Bigdecimal as there is an Constructor of Bigdecimal for the same BigDecimal(String val) ,you are getting error due to total has null value and it can not be converted to BigDecimal. you can do like this

      String total="0.0";
      if(in.getStringExtra(mGrandTotal)!=null && !in.getStringExtra(mGrandTotal).isEmpty()){
       BigDecimal sPrice = new BigDecimal(total);

hope this will work for u...!!!

like image 189
Mantu Singh Avatar answered Feb 28 '23 10:02

Mantu Singh