简体   繁体   中英

NullPointerException - Database locked issue while retrieving data from database and storing into an array

I am trying to retrieve a list of dates(in string format) from a PersonDOB column in database. Then I am encountering Database locked issue. I have declared open() and close() methods for the database. Even though I am getting these errors. I am unable to understand, why I am getting this error. I have read some articles like

Question - 1

Question - 2

Not only these, I have tried solutions given in other questions and articles also. But I am unable to get an appropriate solution, that suits my context. In my entire code, I am using the write operation only once. So, there is no problem of data corruption. Here is the code, where I am getting data from the database.

public String[] getDataInArray() { // get data for list and return in array form
        // TODO Auto-generated method stub
        SQLiteDatabase myDB;
        String[] return_columns;
         try {

              myDB=this.openDataBase();       
              String DOB = "PersonDOB";
            String[] columms = new String[]{ DOB };



            Cursor c = myDB.query("Persons", columms, null, null, null, null, null);

            int iDOB = c.getColumnIndex(DOB);

            int rowcount = 0;
              return_columns = new String[c.getCount()];
            for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
            {
                return_columns[rowcount] = c.getString(iDOB);
                rowcount = rowcount + 1;
            }



                  if(c != null)
                 {
                     myDB.close();
                     c.close();
                  }          


                  }catch(SQLException sqle){

                  throw sqle;

                  }
         for (int i = 0;i<return_columns.length;i++)
            {
                                     Log.v("DOB", return_columns[i]); //I am not
 getting this log message in the logcat.
            }
        return return_columns;
        }

And I get the following errors in the logcat -

09-19 15:58:00.140: W/System.err(7115): java.lang.NullPointerException
09-19 15:58:00.140: W/System.err(7115):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
09-19 15:58:00.140: W/System.err(7115):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
09-19 15:58:00.200: W/System.err(7115):     at com.example.fromstart.adapter.open(adapter.java:30)
09-19 15:58:00.210: W/System.err(7115):     at com.example.fromstart.adapter.getDataInArray(adapter.java:222)

Thanks in advance.

Closing database myDB.close(); is not enough to avoid locks. You need to close the cursors which you are using for database operations before closing the database or when the operation with the cursor is completed. If you are using Thread s please make sure they also must be properly closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM