chris
chris

Reputation: 729

How to handle Number Format Exception in edittext OntextChanged

I have edit text in my app to manipulate product quantity, but the issue if I will change the quantity , on that time getting NPE. to change the quantity in edit text I need to remove existing and need to add a new one, please check following code

viewHolder.edt_product_qty.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


                    quantity_count= Integer.parseInt(viewHolder.edt_product_qty.getText().toString());


                    buyNowList(user_id,"UserCart",0,stList.get(position).getCartList1().getId(),stList.get(position).getCartList1().getProdSku(),stList.get(position).getCartList1().getProdId(),quantity_count,0,qty_status,stList.get(position).getCartList1().getProdPrice());

                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

Upvotes: 0

Views: 388

Answers (3)

Kiryl Tkach
Kiryl Tkach

Reputation: 3614

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    try {
        quantity_count = Integer.parseInt(viewHolder.edt_product_qty.getText().toString()); 
        buyNowList(user_id, "UserCart", 0, stList.get(position).getCartList1().getId(), stList.get(position).getCartList1().getProdSku(), stList.get(position).getCartList1().getProdId(), quantity_count, 0, qty_status, stList.get(position).getCartList1().getProdPrice());    
    } catch (NumberFormatException e) {
        // maybe show some information to user in case of exception
    }
}

Upvotes: 0

John Joe
John Joe

Reputation: 12803

When you remove everything on your editText, editText will return an empty string. Hence you will get NPE and also Number Format Exception.

You can handle like this:

if(!viewHolder.edt_product_qty.getText().toString().equals("")){
   quantity_count= Integer.parseInt(viewHolder.edt_product_qty.getText().toString()); 
   buyNowList(user_id,"UserCart",0,stList.get(position).getCartList1().getId(),stList.get(position).getCartList1().getProdSku(),stList.get(position).getCartList1().getProdId(),quantity_count,0,qty_status,stList.get(position).getCartList1().getProdPrice());
}

To compare initial value with current value

First you need to assign a variable as initial value in beforeTextChanged.

initalValue =viewHolder.edt_product_qty.getText().toString();

Then in onTextChanged:

if(!viewHolder.edt_product_qty.getText().toString().equals("")){
    quantity_count = Integer.parseInt(viewHolder.edt_product_qty.getText().toString()); // 
}if(initalValue>quantity_count)
{
    // code
}else
{
    //code
}

Have a look on this https://stackoverflow.com/a/20278708/5156075

Upvotes: 1

apksherlock
apksherlock

Reputation: 8371

Use try and catch block

try {
  quantity_count = Integer.parseInt(viewHolder.edt_product_qty.getText().toString());
  buyNowList(user_id,"UserCart",0,stList.get(position).getCartList1().getId(),stList.get(position).getCartList1().getProdSku(),stList.get(position).getCartList1().getProdId(),quantity_count,0,qty_status,stList.get(position).getCartList1().getProdPrice());
} catch (NumberformatException e) {
  e.printStackTrace();
}

Upvotes: 2

Related Questions