views:

21

answers:

0

I have an EditText that has a onKeyListener. Whenever a key is pressed, the EditText string is compared with a set of words in a String array, to display a list of matches that begin with the string in EditText01. But the app always force closes. The log says java.lang.NullPointerException.

This is my code:

public class listupdate extends Activity {
    String[] arr = new String[650];
    int i=0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final listupdate ptr = this;
        BufferedReader buf = new BufferedReader(new InputStreamReader(this.getResources().openRawResource(R.raw.wordlist)));
        try {
            String line;
            while((line=buf.readLine()) !=null)
            {
                arr[i++]=line.trim();
                Log.d("WORD", arr[i-1]);
            }
        } catch (IOException e) {

            e.printStackTrace();
        }
        final EditText e1 = (EditText) findViewById(R.id.EditText01);
        final ListView lv = (ListView) findViewById(R.id.ListView01);
        e1.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                String str = e1.getText().toString().toLowerCase();
                String[] matches = new String[650];
                int j=0,k=0;
                while(j<arr.length)
                {
                    if(arr[j].toLowerCase().startsWith(str))
                    {
                        matches[k++]=arr[j];
                    }
                    j++;
                }
                lv.setAdapter(new ArrayAdapter<String>(ptr,R.layout.simplelistitem,matches));
                return true;
            }
        });
    }
}

What am I doing wrong?

related questions