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) {
}
});
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