Abhi Abhi - 1 month ago 9
Java Question

Database table is not showing in SQLite Android

Here I am making the function of Database Cursor which is to be used frequently in the project.

Here is my code in DAL:

public Cursor SelectQuery(String table){
Cursor cursor=SQLiteDb.query(table,null,null,null,null,null,null,null);
return cursor;
}


By debugging SQLiteDB is getting null in the mainactivity.

Code:

Cursor c=dal.SelectQuery(ConstantsValues.TabData);
Log.e("hello111",""+c.getCount());


So as to get total rows But the table is not coming.Here ConstantsValues.TabData is having table name.

Where as in here code:

public ArrayList AllSelectQryForTabEmpData1() {
ArrayList<Employee> data = new ArrayList();

Cursor cursor = SQLiteDb.query(ConstantsValues.TabData, null, null, null, null, null, null, null);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ConstantsValues.ID));
String name = cursor.getString(cursor.getColumnIndex(ConstantsValues.NAME));
String value= cursor.getString(cursor.getColumnIndex(ConstantsValues.VALUE));

data.add(new Employee(id, name,value));
}
cursor.close();
return data;
}


Here is open and close code:

public void OpenDB() {
SQLiteDb = new DBHelper(context).getWritableDatabase();
}

public void CloseDB() {
if (SQLiteDb.isOpen()) {
SQLiteDb.close();
}
}


I am getting Table in SQLiteDb. It seems something is wrong in making Cursor.

Here is my logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abhishek.httpclient, PID: 12538
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abhishek.httpclient/com.example.abhishek.httpclient.MainActivity}: 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, java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2671)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1501)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
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, java.lang.String)' on a null object reference
at com.example.abhishek.httpclient.DAL.SelectQuery(DAL.java:141)
at com.example.abhishek.httpclient.MainActivity.onCreate(MainActivity.java:92)
at android.app.Activity.performCreate(Activity.java:6317)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2532)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2671)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1501)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)

Answer

I think there is something wrong with database connection. Please, follow this tutorial specially DBHelper.java class. How they have made database connection. https://www.tutorialspoint.com/android/android_sqlite_database.htm

SQLiteDatabase db = this.getWritableDatabase();

By using getWritableDatabase(), you have the power to do both read and write operation to your db.

Comments