The main purpose of my project is to create a database automatically when starting up the application for the first time. I am trying to create a simple database through a class extending SQLiteOpenHelper. The following is the code of this class:
public class DatabaseImplementation extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "TestDatabase.db";
private static final int DATABASE_VERSION = 1;
private static final String createDep = "CREATE TABLE tbl_Department("+
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"department_name TEXT NOT NULL);";
private static final String createEmp = "CREATE TABLE tbl_Employee(" +
"_id INTEGER PRIMARY KEY," +
"name TEXT NOT NULL," +
"age INTEGER," +
"department_id INTEGER, "+
"FOREIGN KEY(department_id)"+
"REFERENCES tbl_Department(_id));";
public DatabaseImplementation(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(createDep);
db.execSQL(createEmp);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
//--
}
}
Please not that I do not have any layout declared in my layout/main.xml file as I believe there is no need to for the purpose of this project.
The following is my main activity code:
public class TestingDatabaseMain extends Activity {
DatabaseImplementation db = new DatabaseImplementation(this);
SQLiteDatabase dbWrite = db.getWritableDatabase();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
insertRecords();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.testing_database_main, menu);
return true;
}
private void insertRecords(){
ContentValues values = new ContentValues();
values.put("_id", 1);
values.put("department_name", "HR");
long newrowID = dbWrite.insert("tbl_Department", null, values);
}
}
When I run the application on my Nexus 7, the following error is displayed:
"Unfortunately, Testing Database has Stopped."
and the error in the LogCat is :
07-16 14:14:14.240: E/AndroidRuntime(9979): FATAL EXCEPTION: main
07-16 14:14:14.240: E/AndroidRuntime(9979): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testingdatabase/com.example.testingdatabase.TestingDatabaseMain}: java.lang.NullPointerException
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.os.Looper.loop(Looper.java:137)
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-16 14:14:14.240: E/AndroidRuntime(9979): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 14:14:14.240: E/AndroidRuntime(9979): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 14:14:14.240: E/AndroidRuntime(9979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-16 14:14:14.240: E/AndroidRuntime(9979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-16 14:14:14.240: E/AndroidRuntime(9979): at dalvik.system.NativeStart.main(Native Method)
07-16 14:14:14.240: E/AndroidRuntime(9979): Caused by: java.lang.NullPointerException
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:229)
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
07-16 14:14:14.240: E/AndroidRuntime(9979): at com.example.testingdatabase.TestingDatabaseMain.<init>(TestingDatabaseMain.java:13)
07-16 14:14:14.240: E/AndroidRuntime(9979): at java.lang.Class.newInstanceImpl(Native Method)
07-16 14:14:14.240: E/AndroidRuntime(9979): at java.lang.Class.newInstance(Class.java:1319)
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-16 14:14:14.240: E/AndroidRuntime(9979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-16 14:14:14.240: E/AndroidRuntime(9979): ... 11 more
07-16 14:16:47.030: W/ActivityThread(10487): Application com.example.testingdatabase is waiting for the debugger on port 8100...
I would really appreciate help on this matter, even if someone could link me to other posts having the same problem. Tried looking for a couple of other posts and tried following the answers that were provided to no avail.
Thanks a bunch.
Edited: I am new to android development so do not scold me for any silly errors :)