0

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

                }
            });
Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
chris
  • 699
  • 4
  • 12
  • 35
  • 1
    Please share your code complete and your error , is this a adapter ? – milad salimi Apr 22 '19 at 11:32
  • yes this is adapter..and i am using edittext value to call my api..so to change quantity it is getting null and giving NPE – chris Apr 22 '19 at 11:34
  • 1
    Share your adapter code complete!!! – milad salimi Apr 22 '19 at 11:36
  • https://pastebin.com/t6erHe4x – chris Apr 22 '19 at 11:38
  • 1
    Why don't you catch NumberFormatException of Integer.parseInt? Your app will crash if there will be no number in edt_product_qty (e. g. when user delete all the text). – Kiryl Tkach Apr 22 '19 at 11:38
  • So, if the error is in the adapter, why do you think it is `onTextChanged` error? By the way, in most cases `afterTextChanged` is used. And what are the values of variables there? In what line does the error raise? – CoolMind Apr 22 '19 at 11:39
  • @KirylTkach yes getting numberformat exception..hope you understand the issue..can you give solution – chris Apr 22 '19 at 11:59
  • @chris you can user digits property of edit text to restrict input of the user to only numbers like this, digits='0123456789' then user can not input alphabets. – Kamran Ali Apr 22 '19 at 14:41
  • post the error log – karan Apr 23 '19 at 06:32

3 Answers3

2

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();
}
coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
  • is there any way i can check, updated value greater than existing one..for example..if i change from 2 to 5 so 5 is bigger vice versa – chris Apr 23 '19 at 04:54
  • in your aftertextchange save this quantity_count to another variable and than compare – coroutineDispatcher Apr 23 '19 at 05:15
  • then how will you compare ?? – chris Apr 23 '19 at 06:03
  • if(!viewHolder.edt_product_qty.getText().toString().equals("")){ quantity_count= Integer.parseInt(viewHolder.edt_product_qty.getText().toString()); // } int tempqty=quantity_count; if(what to compare) { } else { } – chris Apr 23 '19 at 06:18
  • please go here https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/1514910/how-to-properly-compare-two-integers-in-java&ved=2ahUKEwjD9LekyeXhAhUHsaQKHQokDRUQjjgwC3oECAMQAQ&usg=AOvVaw0yXaSSGw6an3xyVGR-a_2X&cshid=1556000428770 – coroutineDispatcher Apr 23 '19 at 06:21
  • Bro i know comparision, only issue as per your suggestion it will give same value for both the variables .am i right – chris Apr 23 '19 at 06:39
  • Dude , keep 2 variables at all . One to hold the previous value , and the other to hold the new value and than compare both . – coroutineDispatcher Apr 23 '19 at 06:41
1

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

John Joe
  • 12,412
  • 16
  • 70
  • 135
  • where should i add this condition ? inside ontextchanged or aftertextchanged? – chris Apr 22 '19 at 14:19
  • `onTextCanged`. – John Joe Apr 22 '19 at 14:20
  • getting error java.lang.NumberFormatException: For input string: "" quantity_count= Integer.parseInt(viewHolder.edt_product_qty.getText().toString()); on this line – chris Apr 22 '19 at 14:24
  • is there any way i can check, updated value greater than existing one..for example..if i change from 2 to 5 so 5 is bigger vice versa – chris Apr 23 '19 at 04:54
  • when you want to compare? After one of the button is clicked? – John Joe Apr 23 '19 at 06:06
  • when i change quantity,, like existing is 5 then i change it to 2,, so i need to compare it is higher or lesser than existing one...got it?? – chris Apr 23 '19 at 06:16
  • initial value and quantity_count both will have same value if we do that way – chris Apr 23 '19 at 06:38
  • store initial value in `beforeTextChanged` – John Joe Apr 23 '19 at 06:40
  • hi i am getting one more issue – chris Apr 23 '19 at 11:50
  • What is the issue? – John Joe Apr 23 '19 at 11:55
  • this edittext is product quantity, now i have two button plus and minus beside edittext..on this button i am calling same buyNow method..now if user click on plus and minus and manipulate with quantity of edittext,,it is calling buyNow method twice.. one for button and one which we have used in edittext..getting my point? – chris Apr 23 '19 at 12:13
  • https://pastebin.com/UuJGj5wa check this one..while i call button click listener,,i am calling buynow method, so on that time method inside edittext listener is also calling..that is the issue.. – chris Apr 23 '19 at 12:37
  • where is buyNow method ? You mean buyNowList ? – John Joe Apr 23 '19 at 14:27
  • Need your help https://stackoverflow.com/questions/55946781/how-to-check-app-is-installed-or-not-using-javascript – chris May 02 '19 at 09:20
  • Sorry, not familiar with javascript. I have shared a link to your question. – John Joe May 02 '19 at 11:59
0
@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
    }
}
Kiryl Tkach
  • 3,118
  • 5
  • 20
  • 36