0

I'm getting integers of two EditText and on one of them textWatch is applied, multiplying them and setting text to another EditText. it normally works fine but when one of edit field become empty while hitting backspace app crashes.

protected final TextWatcher getValueWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        int qty = Integer.parseInt(s.toString());
        int unitPrice = Integer.parseInt(unitPriceEdit.getText().toString());
        if (s.length() > 0)
        totalPriceEdit.setText(String.valueOf(qty * unitPrice));
    }
    public void afterTextChanged(Editable s) {
    }
};

Logcat

java.lang.NumberFormatException: Invalid int: "" at 
java.lang.Integer.invalidInt(Integer.java:138) at 
java.lang.Integer.parseInt(Integer.java:358) at 
java.lang.Integer.parseInt(Integer.java:334) at 
com.example.fani.project.MainActivity$2.onTextChanged(MainActivity.java:90) 
at android.widget.TextView.sendOnTextChanged(TextView.java:7679) at 
android.widget.TextView.handleTextChanged(TextView.java:7739) 
5
  • what kind of exception do you get? probably a NumberFormatException? Commented Aug 7, 2015 at 10:44
  • could you please post the logcat.. Commented Aug 7, 2015 at 10:44
  • yes its is NumberFormatException. Commented Aug 7, 2015 at 10:46
  • java.lang.NumberFormatException: Invalid int: "" at java.lang.Integer.invalidInt(Integer.java:138) at java.lang.Integer.parseInt(Integer.java:358) at java.lang.Integer.parseInt(Integer.java:334) at com.example.fani.project.MainActivity$2.onTextChanged(MainActivity.java:90) at android.widget.TextView.sendOnTextChanged(TextView.java:7679) at android.widget.TextView.handleTextChanged(TextView.java:7739) Commented Aug 7, 2015 at 10:49
  • I think you should check the length of string before converting it to integer...because if u try to convert an empty string....it will raise NumberFormatException..😊 Commented Aug 7, 2015 at 10:50

3 Answers 3

3

Replace your code as below

protected final TextWatcher getValueWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before, int count) {
      if (s.length() > 0){
        int qty = Integer.parseInt(s.toString());
        int unitPrice = Integer.parseInt(unitPriceEdit.getText().toString());
        totalPriceEdit.setText(String.valueOf(qty * unitPrice));
      }
    }
    public void afterTextChanged(Editable s) {
    }
};

Check the length of your string before parsing it..

Sign up to request clarification or add additional context in comments.

2 Comments

works like a charm Thank you So much and sorry for posting a silly question but I'm a beginner
Glad that it helped.. :)
1

Modify your onTextChanged():

public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (s.length() > 0){
        int qty = Integer.parseInt(s.toString());
        int unitPrice = Integer.parseInt(unitPriceEdit.getText().toString());
        totalPriceEdit.setText(String.valueOf(qty * unitPrice));
    }
}

2 Comments

works like a charm Thank you So much and sorry for posting a silly question but I'm a beginner
Good to know, can you please accept it as an answer?
0

null pointer error occurs due to s.length > 0 Change this in your code.

 if (s.length() > 0)
    totalPriceEdit.setText(String.valueOf(qty * unitPrice));
 }

TO

if(s != null){

   if (s.length() > 0)
    totalPriceEdit.setText(String.valueOf(qty * unitPrice));
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.