getFilter()’ on a null object reference

  • Post comments:0 Comments

Recently completed SQLite database CRUD operation using  BaseAdapter which later required adding search filter on it, so that the data is visible in listview by filtering. But got  NullPointerException  error like this…

Process: com.example.myapplication, PID: 17694

    java.lang.NullPointerException: Attempt to invoke virtual method ‘android.widget.Filter com.example.crudwithimageupdate.Custom.getFilter()’ on a null object reference

‘android.widget.Filter com.example.crudwithimageupdate.Custom.getFilter”

logcat

 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 17694
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.Filter com.example.crudwithimageupdate.Custom.getFilter()' on a null object reference
        at com.example.crudwithimageupdate.MainActivity2$1.onQueryTextChange(MainActivity2.java:171)
        at android.widget.SearchView.onTextChanged(SearchView.java:1205)
        at android.widget.SearchView.access$2000(SearchView.java:92)
        at android.widget.SearchView$11.onTextChanged(SearchView.java:1689)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7785)
        at android.widget.TextView.handleTextChanged(TextView.java:7845)
        at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9621)
        at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:964)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:515)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:454)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:33)
        at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:685)
        at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:197)
        at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:184)
        at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:286)
        at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5240)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

This is the easy way you can fix it.

This error happens to way when you use the same variable use multiple times at that time you get this key of error.

This variable is used twice in the custom adapter, so got this type of error.

Here this kind of error occurred due to the use of variable Custom adapter means 2-times uses custom adapter and called it. So make sure you don’t use the variable twice. If you need to use it multiple times, use it in the main class and initialize it elsewhere.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments