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?

Venkatesh Goud
  • 616
  • 2
  • 6
  • 22
Jason B.
  • 3
  • 1
  • 3
  • Please edit this new information into your first question: http://stackoverflow.com/q/11764099/1267661 (The "edit" link is in the lower left corner of the question.) – Sam Aug 01 '12 at 17:30
  • I will add that there are other query() methods that don't use all the paramaters so you don't need to have all those nulls. Just advice. And about the possible problem. Is it possible that whatever is being sent into `getAllModelFIlter()` could be null? From what I see, you are sending it a string that you get from a cursor. What if the cursor is length 0. Idk, thats my guess. The logcat errors you provided don't provide much. The only thing that pops out is the nullpointerexception. Oh I know you check `vehicleMake`, but it could potentially be `""`, an empty but not null String. – Andy Aug 01 '12 at 17:57

1 Answers1

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