1

Hi I'm trying to make an app in which there's a search box, I've written some code but, when I try to type the first letter in the AutoCompleteTextView, it gives me an error (java.lang.NullPointerException) without any kind of cause about what caused it.

My mains.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
<AutoCompleteTextView
 android:id="@+id/acTV"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:hint="Scrivi">
<requestFocus />
</AutoCompleteTextView>
<ListView android:id="@+id/list" 
 android:layout_height="wrap_content" 
 android:layout_width="match_parent"
 android:fadingEdge="none" 
 android:fastScrollEnabled="true">
</ListView>
</LinearLayout>

My listitem_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
<TextView
 android:id="@+id/textView1"
 android:text="TextView"
 android:layout_height="wrap_content"
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:layout_width="fill_parent">
</TextView>
<TextView
 android:text="TextView"
 android:id="@+id/textView2"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
</TextView>
</LinearLayout>

My code:

public class Search extends Activity {
    AutoCompleteTextView acTV;
    ListView lview;

    String[] first = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};

    String[] second = {"Uno", "Due", "Tre", "Quattro", "Cinque", "Sei", "Sette", "Otto", "Nove", "Dieci"};
    int textlength = 0;
    ArrayList<String> first_sort = new ArrayList<String>();
    ArrayList<String> second_sort = new ArrayList<String>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mains);

        acTV = (AutoCompleteTextView) findViewById(R.id.acTV);
        lview = (ListView) findViewById(R.id.list);
        lview.setAdapter(new MyCustomAdapter(first, second));

        lview.setClickable(true);
        lview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {

                Intent intent = new Intent(Search.this, Details.class);
                startActivity(intent);
            }

            {
            }
        });

        lview.setTextFilterEnabled(true);
        acTV.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) {

                textlength = acTV.getText().length();
                first_sort.clear();
                second_sort.clear();
                for (int i = 0; i < first.length; i++) {
                    if (textlength <= first[i].length()) {
                        if (acTV.getText().toString().equalsIgnoreCase((String) first[i].subSequence(0, textlength))) {
                            first_sort.add(first[i]);
                            second_sort.add(first[i]);
                        }
                    }
                }

                lview.setAdapter(new MyCustomAdapter(first_sort, second_sort));

            }
        });
    }

    class MyCustomAdapter extends BaseAdapter {

        String[] data_first;
        String[] data_second;

        {

        }

        MyCustomAdapter(String[] first, String[] second) {
            data_first = first;
            data_second = second;
        }

        MyCustomAdapter(ArrayList<String> first, ArrayList<String> second) {
            for (int i = 0; i < first.size(); i++) {
                data_first[i] = first.get(i);
                data_second[i] = second.get(i);
            }

        }

        public int getCount() {
            return data_first.length;
        }

        public String getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = getLayoutInflater();
            View row;

            row = inflater.inflate(R.layout.listitem_row, parent, false);

            TextView textview = (TextView) row.findViewById(R.id.textView1);
            TextView textview1 = (TextView) row.findViewById(R.id.textView2);

            textview.setText(data_first[position]);
            textview1.setText(data_second[position]);

                return (row);

           }
        }
    }
}

And this is the error I receive when I try to type a letter:

12-24 01:37:55.231: E/AndroidRuntime(15241): FATAL EXCEPTION: main 12-24 01:37:55.231: E/AndroidRuntime(15241): java.lang.NullPointerException 12-24 01:37:55.231: E/AndroidRuntime(15241): at it.gogle.com.Search$MyCustomAdapter.(Search.java:114) 12-24 01:37:55.231: E/AndroidRuntime(15241): at it.gogle.com.Search$2.onTextChanged(Search.java:87) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.widget.TextView.sendOnTextChanged(TextView.java:6240) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.widget.TextView.handleTextChanged(TextView.java:6281) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6456) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174) 12-24 01:37:55.231: E/AndroidRuntime(15241): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120) 12-24 01:37:55.231: E/AndroidRuntime(15241): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:247) 12-24 01:37:55.231: E/AndroidRuntime(15241): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:73) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.os.Handler.dispatchMessage(Handler.java:99) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.os.Looper.loop(Looper.java:123) 12-24 01:37:55.231: E/AndroidRuntime(15241): at android.app.ActivityThread.main(ActivityThread.java:4627) 12-24 01:37:55.231: E/AndroidRuntime(15241): at java.lang.reflect.Method.invokeNative(Native Method) 12-24 01:37:55.231: E/AndroidRuntime(15241): at java.lang.reflect.Method.invoke(Method.java:521) 12-24 01:37:55.231: E/AndroidRuntime(15241): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 12-24 01:37:55.231: E/AndroidRuntime(15241): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 12-24 01:37:55.231: E/AndroidRuntime(15241): at dalvik.system.NativeStart.main(Native Method)

Thanks to every person who helps me.

ElOjcar
  • 189
  • 2
  • 3
  • 11
Valerio
  • 51
  • 6

1 Answers1

0

I'm guessing that one of the sort parameters is null.

Confirmed by OP comment, first_sort is null.


As per this blog post, you need to:

  1. Save the values of all your class variables, including static variables in onSaveInstanceState(Bundle) to the bundle.
  2. Restore the variables from the bundle (if it’s not null) in onCreate(Bundle).

See also:


...something like this (not tested):

public class Search extends Activity {
    // snip...

    public void onCreate(Bundle savedInstanceState) {
        // snip...

        if (first_sort == null) {
            first_sort = savedInstanceState.getParcelableArrayList("first_sort");
        }

        if (second_sort == null) {
            second_sort = savedInstanceState.getParcelableArrayList("second_sort");
        }
    }

    // snip...

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList("first_sort", first_sort);
        outState.putParcelableArrayList("second_sort", second_sort);
    }
}
Community
  • 1
  • 1
Matt Ball
  • 332,322
  • 92
  • 617
  • 683
  • Thank you for the answer, this is my first application and I'm not as good as maybe you can imagine. Could you help me writing the missing code or giving a better clue? Thank you again. P.S. I use android:configChanges="keyboard|keyboardHidden|orientation" in Manifest instead of onSave and onRestore. It works also fine. Thanks also for the other help. – Valerio Dec 24 '11 at 01:30
  • When I said if you could help me writing code I meant about how to resolve NullPointerException, if you could. – Valerio Dec 24 '11 at 01:39
  • I've modified this example code [link](http://android-helper.blogspot.com/2011/07/android-search-in-custom-listview.html?showComment=1324684325255#c535218314476375289) for my needs, but I don't understand why my code doesn't work. I have two String[] instead of a String[] and an int[] as in the example. Maybe is this what causes the error? – Valerio Dec 26 '11 at 22:47