3

Here is My Code Snippet which perform a Search operation based on the text entered in a EditText Box, Please have a look on it...

ed.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

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

        public void onTextChanged(CharSequence s, int start, int before,
        int count) {
        b4.setVisibility(b4.VISIBLE);   
        textlength=ed.getText().length();
        arr_sort.clear();
        for(int i=0;i<lv_arr.length;i++)
        {
            if(textlength<=lv_arr[i].length())
            {
            boolean b=ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength));
            if(b)
                {
                    arr_sort.add(lv_arr[i]);
                    //Log.i("In If:",arr_sort.toString());
                }
            }
        }
        lv1.setAdapter(new ArrayAdapter<String>(BasicsActivity.this,R.layout.rowlayout2 ,R.id.label, arr_sort));
        //Log.i("Step6-","ok");
        }
        });

Now the Problem is when I write anything on the EditText View while Running it shows a RunTime FATALException with some NullPointerException..Please suggest me some some way out of here.. The Excpetions are as follows..

02-02 13:29:48.577: ERROR/AndroidRuntime(541): FATAL EXCEPTION: main
02-02 13:29:48.577: ERROR/AndroidRuntime(541): java.lang.NullPointerException
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at com.android.desitranslation.BasicsActivity$1.onTextChanged(BasicsActivity.java:65)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.widget.TextView.sendOnTextChanged(TextView.java:6295)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.widget.TextView.handleTextChanged(TextView.java:6336)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6485)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:195)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.widget.TextView.doKeyDown(TextView.java:4433)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.widget.TextView.onKeyDown(TextView.java:4267)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.view.KeyEvent.dispatch(KeyEvent.java:1256)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.view.View.dispatchKeyEvent(View.java:3855)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1687)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1120)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.app.Activity.dispatchKeyEvent(Activity.java:2073)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.os.Looper.loop(Looper.java:123)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at android.app.ActivityThread.main(ActivityThread.java:3647)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at java.lang.reflect.Method.invokeNative(Native Method)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at java.lang.reflect.Method.invoke(Method.java:507)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-02 13:29:48.577: ERROR/AndroidRuntime(541):     at dalvik.system.NativeStart.main(Native Method)
8
  • 3
    It would help if you told us which line was line 65 (that's the line that generated the null pointer exception). You're using a lot of external variables in onTextChanged(); probably one of them, or one of the array slots, is null. Commented Feb 2, 2011 at 8:27
  • I second Ted Hopp's request for telling us which one is line 65, and I dare to guess thats going to be the one with ed.getText().toString() Commented Feb 2, 2011 at 8:31
  • The Line no 65 is the if condition i.e.; if(textlength<=lv_arr[i].length()){...} Commented Feb 2, 2011 at 8:36
  • 1
    what is lv_arr? How do you fill it? Commented Feb 2, 2011 at 8:45
  • What Vladimir is getting at is that either lv_arr or lv_arr[i] is null at that point. Use the debugger to find out which, or just examine your code to see why one of these isn't being given a value. Maybe you need to test (if (lv_arr != null && lv_arr[i] != null && ...)). Commented Feb 2, 2011 at 8:48

1 Answer 1

1

Here in the Line No 65, which is nothing but a If(condition) i.e.;

if(textlength<=lv_arr[i].length())
{..}

gets the Null Pointer Exception Because the value of the List lv_arr[i] is returning a Null Value which causes the runtime FATAL EXCEPTION with a java.lang.NullPointerException.

So When I review my code I found that there are some conflicts in the List Size which is in one place 44 and in another place it is 53 wherefore the when the value of i is 44 the list lv_arr[i]=null and comparison with null value gives rise to this Exception...

I successfully cleared it out in my code thanks to all of you..

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

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.