At the first initialization of the dbHelper object extends a SQLiteOpenHelper, the error java.lang.NullPointerException comes out, and when you call it again, there is no error.

public DBHelper(Context context) throws IOException { super(context, DB_NAME, null, 1); System.out.print(context.getFilesDir().getPath()); DB_PATH = context.getFilesDir().getPath() + "/" + DB_NAME; this.mycontext = context; boolean dbExists = checkdatabase(); if (dbExists) opendatabase(); else createdatabase(); //dbIsReady = true; } 

createdatabase () opendatabase () opendatabase () will not describe, because Speaker names Is there an adequate way to check if the database exists on the device or somehow suspend the main thread while the database is being copied to the device

I just made it clumsy, with a boolean variable in the dbHelper class, which becomes true at the end of the constructor (which copies the database to the device if there is none), and in the main thread

 ... do {} while(db.isDbReady()); ... 

DB its own, made on the computer. I added it to the project's assets and I need the database to be copied to the device, for example 2gs, by cities and in each city of the database of institutions, transport, etc. Here I am going to make a DB cafe in the city, and therefore I do DB on a computer in order not to bathe with ContentValues

Logcat

 04-04 12:21:23.670 14687-14687/ru.vladdev.andrmenu E: FATAL EXCEPTION: main Process: ru.vladdev.andrmenu, PID: 14687 java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.vladdev.andrmenu/ru.vladdev.andrmenu.CafeMenuActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2538) at android.app.ActivityThread.access$1000(ActivityThread.java:168) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1433) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5589) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference at ru.vladdev.andrmenu.CafeMenuActivity.onCreate(CafeMenuActivity.java:32) at android.app.Activity.performCreate(Activity.java:5993) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2538) at android.app.ActivityThread.access$1000(ActivityThread.java:168) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1433) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5589) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751) 
  • And where exactly does a NullPointerException occur? Without this, and output to the logs instead of help, you get only fortune-telling on the coffee grounds. - Yuriy SPb
  • At a minimum, you can check if the database itself exists in the file system, namely, to check the existence of the database file, and so I agree with Yuri, you need more specifics, even if you want to give logs - BORSHEVIK
  • The database is created via the CREATE TABLE request, or are you already copying from somewhere? If the first, then there is no need to check the presence of the database, since with a correctly implemented SQLiteOpenHelper successor, in particular the onCreate and onUpgrade , if there is no database, they will be called automatically - temq
  • Added a question - Herrgott

1 answer 1

You can view and copy the database via Android Device Monitor.

Pay attention to methods SQLiteOpenHelper

 onCreate () - здесь создаешь БД onUpgrade () - здесь обновляешь БД для след релиза