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