1

I have a ListView that fills from existing database, when user clicked on each item, one of the other columns, should be displayed on second Activity (Like dictionary App)

But I can't execute any queries on second activity.

I'm using SqliteAssetHelper library.

Database class:

public class MyDatabase extends SQLiteAssetHelper {

private static final String DATABASE_NAME    = "database.db";
private static final int    DATABASE_VERSION = 1;


public MyDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

}

On the first activity, listView filled from the database:

 private void fillFromDb() {
    G.itemLists.clear();
    Cursor cursor = mydb.rawQuery("select * from test order by title limit 0,50 ", null);
    while (cursor.moveToNext()) {
        StructNote test1 = new StructNote();
        test1.title = cursor.getString(cursor.getColumnIndex("title"));
        G.itemLists.add(test1);
    }
    cursor.close();
    mydb.close();
    adapter.notifyDataSetChanged();
    }

but the problem is in the second activity; I can't execute any queries!

public class ActivityResult extends Activity {

private MyDatabase    MyDataBase;
public SQLiteDatabase mydb;


@Override
protected void onResume() {
    G.currentActivity = this;
    super.onResume();
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);

    Cursor cursor = mydb.rawQuery("SELECT * FROM test ", null);
    while (cursor.moveToNext()) {
        try {
            Log.i("LOG", cursor.getString(cursor.getColumnIndex("title")));
        }
        catch (Exception e) {}
    }
    cursor.close();
    mydb.close();

}

 }

logcat: NullPointerException

Many thanks...

My question was not duplicate! Other answers that I found about NullPointerException were not fully addressing my question :)

Because my problem and solution was only in use of SqliteAssetHelper library, that I couldn't find anything about it ;)

Emad
  • 588
  • 1
  • 9
  • 22
  • Any specific reason not to use SQLiteOpenHelper as shown in the [official doc](http://developer.android.com/training/basics/data-storage/databases.html#DbHelper)? Also are you using getReadableDatabase? – Kerem Jul 12 '15 at 20:59

1 Answers1

1

According to SqliteAssetHelper library docs:
https://github.com/jgilfelt/android-sqlite-asset-helper

MyDataBase = new MyDatabase(this);

should set in OnCreate method...

so my second activity,should change to:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);

MyDataBase = new MyDatabase(this); // <-----
mydb = MyDataBase.getWritableDatabase(); //  <-----

Cursor cursor = mydb.rawQuery("SELECT * FROM test ", null);
while (cursor.moveToNext()) {
    try {
        Log.i("LOG", cursor.getString(cursor.getColumnIndex("title")));
    }
    catch (Exception e) {}
}
cursor.close();
mydb.close();

}
Emad
  • 588
  • 1
  • 9
  • 22