-1

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) 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Abhi
  • 385
  • 1
  • 4
  • 13

2 Answers2

3

You need to instantiate SQLiteDatabase first, something like this:

// Create and/or open the database for writing
SQLiteDatabase db = getWritableDatabase();

UPDATE:

For a simple fix, you can change your openDB() method to (note: not tested):

public void openDB() {
  if(sqliteDB == null) {
    sqliteDB= new DBHelper(context).getWritableDatabase();
  } else {
    if(!sqliteDB.isOpen(){
       // reopen again. 
       // NOTE: This should be changed to the method which you use to open the db.
       sqliteDB= new DBHelper(context).getWritableDatabase();
    }
  }
}

Then call it every time you use the SQLiteDb, something like this:

openDB();
Cursor cursor = SQLiteDb.query(ConstantsValues.TabData, null, null,
                                null, null, null, null, null);
...
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
2

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.

minhazur
  • 4,928
  • 3
  • 25
  • 27
  • Well my other code in AllSelectQryForTabEmpData1() its working fine.....but not this yet i will try – Abhi Oct 26 '16 at 07:45
  • I think, those code snippet are taken from two different java class. May be, in one you have created connection but in other not. @Abhi – minhazur Oct 26 '16 at 07:51
  • Yes i had just initialised it in other java class...**arr_emp** = ban.AllSelectQryForTabEmpData1(); – Abhi Oct 26 '16 at 07:53
  • please, make sure you have established connection before using SQLiteDB. If needed, call openDb() method again. – minhazur Oct 26 '16 at 07:55
  • 1
    you were right Thank you :) 1 vote for that.. :) – Abhi Oct 26 '16 at 07:58