0

My frist post only had less than half my text hence the second (complete) post.

I'm working on a test app using sqlite to populate two AutoCompleteTextView's i'm using car make and model for the test

AutoComplete's:

makeAutocomplete

modelAutocomplete

makeAutocomplete's list is populated from a sql query and it works fine

the second is populated when the make is selected

makeAutoComplete.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            final String[] makeSelected = {arg0.getItemAtPosition(arg2).toString()};
            final String[] modelDeal = sqlDBModel.getAllModelFilter(makeSelected);
            ArrayAdapter<String> modelAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, modelDeal);
            initModelAutoComplete(modelAdapter);

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

initModelAutoComplete Declaration

public void initModelAutoComplete(ArrayAdapter<String> adapter){


    //adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, list);
    modelAutoComplete.setAdapter(adapter);
    modelAutoComplete.setThreshold(1);
    modelAutoComplete.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
            arg0.getItemAtPosition(arg2);
        }
    });
}

SQLiteModelSearch.getAllModelFilter Declaration

    public String[] getAllModelFilter(String[] vehiclemake){

    if(vehiclemake != null){

        Cursor cursor = this.sqliteDBInstance.query(DB_MAKEMODEL_TABLE,
                                                    new String[]{DB_COLUMN_MAKE, DB_COLUMN_MODEL},
                                                    DB_COLUMN_MAKE+"=?",
                                                    vehiclemake,
                                                    null,
                                                    null,
                                                    null,
                                                    null);


        if( cursor != null){
            String[] str = new String[cursor.getCount()];

            int i = 0;
            while(cursor.moveToNext()){
                str[i] = cursor.getString(cursor.getColumnIndex(DB_COLUMN_MODEL));
                i++;
            }
            return str;
        } else {
            Log.i("vehiclemake = ", "NULL");
            return new String[]{};
        }
    }

    return new String[]{};
}

LogCat

07-31 13:00:19.631: E/AndroidRuntime(1302): at android.app.ActivityThread.main(ActivityThread.java:3683)

07-31 13:00:19.631: E/AndroidRuntime(1302): at java.lang.reflect.Method.invokeNative(Native Method) 07-31 13:00:19.631: E/AndroidRuntime(1302): FATAL EXCEPTION: main

07-31 13:00:19.631: E/AndroidRuntime(1302): java.lang.NullPointerException

07-31 13:00:19.631: E/AndroidRuntime(1302): at com.myapp.sqltest.database.SQLiteModelSearch.getAllModelFilter(SQLiteModelSearch.java:100)

07-31 13:00:19.631: E/AndroidRuntime(1302): at com.myapp.sqltest.activity.addVehicleActivity$2.onItemSelected(addVehicleActivity.java:62)

07-31 13:00:19.631: E/AndroidRuntime(1302): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)

07-31 13:00:19.631: E/AndroidRuntime(1302): at android.widget.AdapterView.access$200(AdapterView.java:42)

07-31 13:00:19.631: E/AndroidRuntime(1302): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)

07-31 13:00:19.631: E/AndroidRuntime(1302): at android.os.Handler.handleCallback(Handler.java:587)

07-31 13:00:19.631: E/AndroidRuntime(1302): at android.os.Handler.dispatchMessage(Handler.java:92)

07-31 13:00:19.631: E/AndroidRuntime(1302): at android.os.Looper.loop(Looper.java:130)

07-31 13:00:19.631: E/AndroidRuntime(1302): at java.lang.reflect.Method.invoke(Method.java:507)

07-31 13:00:19.631: E/AndroidRuntime(1302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

07-31 13:00:19.631: E/AndroidRuntime(1302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

07-31 13:00:19.631: E/AndroidRuntime(1302): at dalvik.system.NativeStart.main(Native Method)

I can see that the exception is being thrown at:

Cursor cursor = this.sqliteDBInstance.query(DB_MAKEMODEL_TABLE,
                                                    new String[]{DB_COLUMN_MAKE, DB_COLUMN_MODEL},
                                                    DB_COLUMN_MAKE+"=?",
                                                    vehiclemake,
                                                    null,
                                                    null,
                                                    null,
                                                    null);

but can't tell why, i've watched all the variables going into the functions and none of them are null?

Jason B.
  • 3
  • 1
  • 3

2 Answers2

0

I doubt believe OnItemSelected will ever be called by an AutoCompleteTextView, when the user clicks an item in the dropdown list the AutoComplete will only trigger an OnItemClick event.

Try moving your OnItemSelectedListener code into the OnItemClickListener.

Addition

SQLiteDatabase.query() will always return a valid Cursor, you need to check the size of the Cursor to see if it is valid:

    if(cursor != null){ // Not applicable

Try:

if(cursor.getCount() > 0)

Also you don't particularly need to convert your Cursor into a String array, a SimpleCursorAdapter will handle the details of this. But your approach will work as well (with a few changes).

Null Pointer Exception

If you are receiving a NPE here:

Cursor cursor = this.sqliteDBInstance.query(...);

Then simply sqliteDBInstance is null. Where do you initialize sqliteDBInstance? Do you have an open() method in SQLiteModelSearch, if so did you call it?

Sam
  • 86,580
  • 20
  • 181
  • 179
  • this was a half post? not sure what happend the full post is here: http://stackoverflow.com/questions/11764425/autocompleteview-crashing-at-cursor – Jason B. Aug 01 '12 at 17:28
  • @JasonB. I updated my answer, I had missed that you posted where the exception is thrown. – Sam Aug 01 '12 at 18:40
  • OnItemSelectedListener defiantly works I'm tracing through it. I've now found the NPE is being thrown at: modelAutoComplete.setAdapter(adapter); still debugging will post results – Jason B. Aug 02 '12 at 02:14
  • If the NPE is there then simply `modelAutoComplete` is null, make sure you initialized it. I'm curious how OnItemSelectedListener is working, I believe you but can't duplicate at it the moment. Also you are trying to use OnItemClickListener for `modelAutoComplete`, I don't really know how there is a difference between these for an AutoCompleteTextView... – Sam Aug 02 '12 at 03:16
  • the strange thing is when debugging, i can inspect "adapter" and see the car models in mObjects.a[] . so not sure why its throwing a NPE. I'm not sure the difference between OnItemSelectedListener and OnItemClickListener, but i remember when I was originally trying to catch the selected item OnItemClickListener didn't give me the desired behavior, or an error. I can't remember which now. – Jason B. Aug 02 '12 at 12:32
  • you where correct about OnItemSelectedListener, it was working in the emulator but not on the phone. To be more specific it would only work in the emulator if i used the arrow keys to select the item, then press the enter key – Jason B. Aug 04 '12 at 14:34
0

I've figured it out. the issue was that I had defined

public AutoCompleteTextView modelAutoComplete;
public AutoCompleteTextView makeAutoComplete;

Then then in function onCreate

I had defined:

AutoCompleteTextView modelAutoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteModel);
AutoCompleteTextView makeAutoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteModel);

I overlooked variable scope since both makeAutoComplete and modelAutoComplete where defined twice, one as a global class variable, and the second as a local function variable.

since the global variable was only defined and not initialized. The function initModelAutoComplete() was referencing the global public variable modelAutoComplete causing the NPE exception.

Jason B.
  • 3
  • 1
  • 3