0

I am having a problem using SQLite and creating a database within my android app. I was hoping someone here could point out something that I am doing wrong when simply trying to create the database. Here is the code:

Inside DBContract class:

public final class DBContract {

public DBContract() { }    

public static abstract class DBEntry implements BaseColumns {

    public static final String TABLE_WORKOUTS = "Workouts";
    public static final String ENTRY_ID = "id";
    public static final String ENTRY_NAME = "Exercise";
    public static final String ENTRY_DESCRIPTION = "Description";
    public static final String ENTRY_SETS = "Sets";
    public static final String ENTRY_REPS = "Reps";
    public static final String ENTRY_WEIGHT = "Weight";


}

private static final String TEXT_TYPE = "TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + DBEntry.TABLE_WORKOUTS + " (" +
                DBEntry.ENTRY_ID + " INTEGER PRIMARY KEY," +
                DBEntry.ENTRY_NAME + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_DESCRIPTION + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_SETS + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_REPS + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_WEIGHT + TEXT_TYPE  + " )";

private static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + DBEntry.TABLE_WORKOUTS;


public static class DBHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "DigiBottle.db";

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

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }

    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
  }
}

And this is what I do to try to access the database:

public class MainActivity extends AppCompatActivity {

private BluetoothAdapter mBluetoothAdapter;
private BroadcastReceiver mReceiver;
private BluetoothSocket mmSocket;

private static int REQUEST_ENABLE_BT = 1;
private int hc05Index;
private String hc05Address;
private int numOfReadBytes;
private int readBuffer;
private ArrayList<String> deviceNameList = new ArrayList<String>();
private ArrayList<String> deviceAddressList = new ArrayList<String>();

DBContract.DBHelper mDBHelper = new DBContract.DBHelper(getBaseContext());

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    SQLiteDatabase db = mDBHelper.getWritableDatabase();

NOTE: I cut off the rest of the onCreate() function of the main activity as to not add unnecessary code to have to scroll through.

So inside the Main Activity I create an instance of my DBHelper class and inside the onCreate function of the main activity I call the getWriteableDatabase() function to either get an existing database or create a new one if it hasn't been created. It is my understanding that this function will call the onCreate() function of my DBHelper class which should execute the Create Table SQL Command that is written and create the database. For some reason the getWriteableDatabase() function is forcing the app to close.

Any help would be greatly appreciated!

Thanks in advance!!

EDIT: Here is the log:

02-01 22:04:48.681 21782-21782/com.example.rpholmes.digibottle E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.rpholmes.digibottle, PID: 21782
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rpholmes.digibottle/com.example.rpholmes.digibottle.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' 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 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                                 at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                                                                                 at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                 at com.example.rpholmes.digibottle.MainActivity.onCreate(MainActivity.java:45)
                                                                                 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) 

02-01 22:04:48.705 21782-21782/? I/Process: Sending signal. PID: 21782 SIG: 9

RupaRupa
  • 35
  • 1
  • 1
  • 7
  • post your Log statements. – Mohd Asif Ahmed Feb 02 '16 at 04:40
  • Check log : there is null pointer exception at location com.example.rpholmes.digibottle.MainActivity.onCreate(MainActivity.java:45) – Rakesh Soni Feb 02 '16 at 05:11
  • @Rakesh Soni there was a context null error. I moved the following statement to be inside the Main Activity onCreate() function, and it seems to be okay. DBContract.DBHelper mDBHelper = new DBContract.DBHelper(this.getBaseContext()); – – RupaRupa Feb 02 '16 at 05:22

2 Answers2

0

SQL queries require proper white spacing. Modify the following lines in DBContract as...

private static final String TEXT_TYPE = " TEXT ";
private static final String COMMA_SEP = ", ";

(Please post your Log statements if error still persists)

  • @Shahzar_Ahmed I tried making those changes and the error is persisting. I added the log file for you to view. Any more suggestions? – RupaRupa Feb 02 '16 at 05:07
  • @Shahzar_Ahmed I moved this statement to be inside the Main Activity onCreate() function, and it seems to be okay. DBContract.DBHelper mDBHelper = new DBContract.DBHelper(this.getBaseContext()); – RupaRupa Feb 02 '16 at 05:15
0

you are trying to insert the values before creation of table, try to Add SQLiteDatabase db = mDBHelper.getWritableDatabase(); in DBHelper class only. So that after creation of table insertion can be done.

 public void InsertValues(){
        db =this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(DBEntry.ENTRY_NAME,"adc");
    }