Gaurav Gaurav - 1 month ago 8
Java Question

Why is my EditText TextWatcher giving me null errors from emulator keyboard?

The error I am trying to figure out is coming from my textwatcher but I can't figure out how to fix it.

public class TipActivity extends Activity implements View.OnClickListener, TextWatcher{
EditText[] values;
Button reset, exit;
TextWatcher textWatcher;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

values = new EditText[4];
values[0] = (EditText)findViewById(R.id.value);
values[1] = (EditText)findViewById(R.id.tip);
values[2] = (EditText)findViewById(R.id.amount);
values[3] = (EditText)findViewById(R.id.fullAmount);
reset = (Button)findViewById(R.id.reset);
exit = (Button)findViewById(R.id.exit);
for(int i = 0; i < values.length; i++)
values[i].setOnClickListener(this);
reset.setOnClickListener(this);
exit.setOnClickListener(this);
values[0].addTextChangedListener(textWatcher);
}
@Override
public void afterTextChanged(Editable e) {
if (!values[0].getText().toString().equals(null)) {
String bill = values[0].getText().toString();
int billValue = Integer.parseInt(bill);
Toast.makeText(getApplicationContext(), "Bill Amount: " + bill, Toast.LENGTH_SHORT).show();
values[3].setText(billValue);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){}
}


When I enter a value in the EditText values[0] on my eclipse emulator, I get these errors. Here is my logcat:

10-24 13:23:13.077: D/AndroidRuntime(4277): Shutting down VM
10-24 13:23:13.077: W/dalvikvm(4277): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-24 13:23:13.127: E/AndroidRuntime(4277): FATAL EXCEPTION: main
10-24 13:23:13.127: E/AndroidRuntime(4277): java.lang.NullPointerException
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.widget.TextView.sendBeforeTextChanged(TextView.java:6280)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.widget.TextView.access$700(TextView.java:192)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.widget.TextView$ChangeWatcher.beforeTextChanged(TextView.java:6478)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.sendTextWillChange(SpannableStringBuilder.java:878)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:279)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:654)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:180)
10-24 13:23:13.127: E/AndroidRuntime(4277): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
10-24 13:23:13.127: E/AndroidRuntime(4277): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:273)
10-24 13:23:13.127: E/AndroidRuntime(4277): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:75)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.os.Looper.loop(Looper.java:123)
10-24 13:23:13.127: E/AndroidRuntime(4277): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-24 13:23:13.127: E/AndroidRuntime(4277): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 13:23:13.127: E/AndroidRuntime(4277): at java.lang.reflect.Method.invoke(Method.java:507)
10-24 13:23:13.127: E/AndroidRuntime(4277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-24 13:23:13.127: E/AndroidRuntime(4277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-24 13:23:13.127: E/AndroidRuntime(4277): at dalvik.system.NativeStart.main(Native Method)


What can I do to fix this?

Answer

your textWatcher is null. You declare it TextWatcher textWatcher; but you dont initialize it. Try this way:

values[0].addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });