0

I'm working on an app which is supposed to inflate and AutoCompleteTextView with queries from a database. The problem I have is that when I call the method to inflate the TextView I can't access the database.

TextChanged Listener (Taken from CustomAutocompleteTextChangedListener.java) :

    @Override
public void onTextChanged(CharSequence userInput, int start, int before, int count) {

    //Log user input
    Log.d(TAG,"User input : " + userInput);

    Vindsiden vindsiden = new Vindsiden();

    //Query the database based on the user input
    vindsiden.item = vindsiden.getItemsFromDb(userInput.toString());


    //Update the adapter
    vindsiden.myAdapter.notifyDataSetChanged();
    vindsiden.myAdapter = new ArrayAdapter<String>(vindsiden, android.R.layout.simple_dropdown_item_1line,vindsiden.item);
    vindsiden.AcTv_sok.setAdapter(vindsiden.myAdapter);


}

Which then calls the method getItemsFromDb

 public List<Sted> read(String searchTerm) {
    List<Sted> recordList = new ArrayList<Sted>();
    //Select query
    String sql = "";
    sql += "SELECT * FROM" + TABLE_PLACES;
    sql += " WHERE " + COLUMN_NAME + " LIKE '%" + searchTerm + "%'";
    sql += " ORDER BY " + COLUMN_ID + " DESC";
    sql += " LIMIT 0,5";


    //The line under is what is returning null
    SQLiteDatabase database = this.getWritableDatabase();


    //execute the query
    Cursor cursor = database.rawQuery(sql, null);

    //Looping rows and adding to the list

    if (cursor.moveToFirst()) {

        do {
            String objectName = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
            Sted sted = new Sted();

            recordList.add(sted);
        } while (cursor.moveToNext());
    }
    cursor.close();
    database.close();
    return recordList;

}

I have several items that accesses the database the other methods work as for example the method below :

   public Sted findPlace(String placeName) {

    String query = "Select * FROM " + TABLE_PLACES + " WHERE " + COLUMN_NAME + " =  \"" + placeName + "\"";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(query, null);
    Sted sted = new Sted();

    if (cursor.moveToFirst()) {
        cursor.moveToFirst();
        sted.set_id(Integer.parseInt(cursor.getString(0)));
        sted.set_stedsNavn(cursor.getString(1));
        sted.set_url(cursor.getString(2));
        cursor.close();

    } else {
        sted = null;

    }
    database.close();
    return sted;


}

Stacktrace :

02-01 10:50:08.201  14164-14164/com.vindsiden.windwidget E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.vindsiden.windwidget, PID: 14164
java.lang.NullPointerException
        at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:256)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
        at com.vindsiden.windwidget.config.DatabaseHandler.read(DatabaseHandler.java:143)
        at com.vindsiden.windwidget.Vindsiden.getItemsFromDb(Vindsiden.java:483)
        at com.vindsiden.windwidget.config.CustomAutocompleteTextChangedListener.onTextChanged(CustomAutocompleteTextChangedListener.java:38)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
        at android.widget.TextView.handleTextChanged(TextView.java:7467)
        at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9183)
        at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
        at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:675)
        at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:437)
        at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:333)
        at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

The full code is avalible as : https://github.com/danielgolan/Vindsiden/tree/Daniel2

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dukes
  • 342
  • 1
  • 3
  • 14
  • what is line 38 `CustomAutocompleteTextChangedListener.java`? – Raghunandan Feb 01 '14 at 10:02
  • Its from the example i used. It suggested to create a CustomAutocompleteTextChangedListener to call the database and inflate the AutoComplete list correctly. (Its the first block of code) Is there maybe a smarter way to do this ? – Dukes Feb 01 '14 at 10:05
  • From your github link you are isntantiating a activity class wrong. and this `vindsiden` is null i guess – Raghunandan Feb 01 '14 at 10:08
  • How should i do this correct ? – Dukes Feb 01 '14 at 10:12
  • Don't instantiate activity class and what is line 138? – Raghunandan Feb 01 '14 at 10:13
  • I dont quite get what you meen ? Could you please post an example ? – Dukes Feb 01 '14 at 10:22
  • 1
    Read Raghav Soods answer you will understand http://stackoverflow.com/questions/14956018/can-i-create-the-object-of-a-activity-in-other-class – Raghunandan Feb 01 '14 at 10:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46583/discussion-between-dukes-and-raghunandan) – Dukes Feb 01 '14 at 10:41

1 Answers1

0

The Context you passed to SQLiteOpenHelper parent constructor was null.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • May sound silly. But how do i fix this ? Jep the code isnt really cleaned up yet.. – Dukes Feb 01 '14 at 10:11
  • This is what causes the stacktrace you're seeing. The code in the question doesn't show how you create the helper. – laalto Feb 01 '14 at 10:14