-2

It's giving me null pointer exception i have initialized added textwatcher but still i am having this exception. Am I forgetting some thing.
Any Help is Appreciated.

    EditText editText = (EditText) dialogList.findViewById(R.id.search);
editText.addTextChangedListener(mSearchTw);

mSearchTw = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,int after) {
        pokeAdapter.getFilter().filter(s);
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
};
Farmer
  • 4,093
  • 3
  • 23
  • 47
Asad AndyDev
  • 481
  • 5
  • 23
  • Why you do like this, you can do simply like this http://stackoverflow.com/questions/7432083/how-to-use-edittext-ontextchanged-event-when-i-press-the-number – Farmer Aug 13 '16 at 15:06

3 Answers3

1

Not initiated properly ,

You can check using editText.getText().toString() If through error then it means not initiate properly

Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
1

Of course, it will give you Null Pointer, you basically add a null object at the following line

editText.addTextChangedListener(mSearchTw);

You add it before initializing it.

To fix, just move it after the initiation, i.e. after mSearchTw = new TextWatcher() {...};

That is because those statements are executed sequentially.

Community
  • 1
  • 1
Dimitar
  • 4,402
  • 4
  • 31
  • 47
1

As @user8 gives the answer you can pass the null object of class.You just change like this

EditText editText = (EditText)findViewById(R.id.editText);
        TextWatcher mSearchTw  = new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {
                pokeAdapter.getFilter().filter(s);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        };

        editText.addTextChangedListener(mSearchTw);
Farmer
  • 4,093
  • 3
  • 23
  • 47