0

I am using the Android SQLiteAssetHelper to load a populated .db file into my android app. However using the following code it doesn't actually make the database.

public class DataBaseHelper extends SQLiteAssetHelper {

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

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


public Question getRandQuestion(String category){

    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String sqlTables = category;

    qb.setTables(sqlTables);
    Cursor c=db.rawQuery("SELECT * FROM " + category + " ORDER BY RANDOM() LIMIT 1", null);
    Question question = new Question(Integer.parseInt(c.getString(0)), c.getString(1),
            c.getString(2), Uri.parse(c.getString(3)));
    return question;
}

}

I keep getting the following error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference

I call the DataBaseHelper constructor within another class like so:

Play Class:

    private DataBaseHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);
    db=new DataBaseHelper(this);

I then access the getRandQuestion method within DataBaseHelper but i get the error stated above when trying to open a readable database.

Any idea on what is going wrong?

Thanks in advance

EDIT:

Enitre stack trace:

01-29 18:27:02.228 3867-3867/com.example.anthony.triviaapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.anthony.triviaapp, PID: 3867
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.anthony.triviaapp/com.example.anthony.TriviaApplication.Play}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference
                                                                             at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:178)
                                                                             at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254)
                                                                             at com.example.anthony.TriviaApplication.DataBaseHelper.getRandQuestion(DataBaseHelper.java:44)
                                                                             at com.example.anthony.TriviaApplication.Play.CreateQuiz(Play.java:83)
                                                                             at com.example.anthony.TriviaApplication.Play.onCreate(Play.java:30)
                                                                             at android.app.Activity.performCreate(Activity.java:6237)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Wuffle
  • 73
  • 1
  • 14
  • Please post the entire stack trace. – CommonsWare Jan 29 '16 at 18:29
  • 1
    @user5792452 Are you sure the file path is correct? `assets/databases/quiz.db` – PPartisan Jan 29 '16 at 19:24
  • Check LogCat above the stack trace and look for messages, possibly at `warning` severity, that refer to "database". It feels like the attempt to unpack your database is failing, perhaps for the reason mentioned by PPartisan. The log message may give you more clues. – CommonsWare Jan 29 '16 at 19:26
  • Note that `SQLiteAssetHelper` is **not** a standard Android class. – Code-Apprentice Jan 29 '16 at 19:34
  • the path is correct, and the log says something about file being encrypted or not a database or possible corruption. – Wuffle Jan 29 '16 at 19:59

0 Answers0