NullPointerException - 从数据库检索数据并存储到数组时出现数据库锁定问题 [英] NullPointerException - Database locked issue while retrieving data from database and storing into an array

查看:128
本文介绍了NullPointerException - 从数据库检索数据并存储到数组时出现数据库锁定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从数据库中的PersonDOB列检索日期列表(以字符串格式)。然后我遇到数据库锁定问题。我已经声明了数据库的open()和close()方法。即使我得到这些错误。我无法理解,为什么我得到这个错误。
我已经阅读了一些文章



问题 - 1



问题 - 2



不仅这些,我已经尝试过其他问题和文章中给出的解决方案。但我无法得到一个适当的解决方案,适合我的上下文。
在我的整个代码中,我只使用写操作一次。所以,没有数据损坏的问题。
这是我从数据库获取数据的代码。

  public String [] getDataInArray //获取列表数据并返回数组形式
// TODO自动生成方法存根
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 {
Log.v(DOB,return_columns [i] ; //我不是
在logcat中获取此日志消息。
}
return return_columns;
}

我在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 ):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.java30)
09-19 15:58:00.210:W / System.err(7115):at com.example.fromstart.adapter.getDataInArray(adapter.java:222)$ b $

解决方案

div>

关闭数据库 myDB.close(); 不足以避免锁。您需要在关闭数据库之前或在使用光标操作时关闭数据库操作所使用的游标完成了。如果您使用主题,请确保它们也必须正确关闭。


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 Threads please make sure they also must be properly closed.

这篇关于NullPointerException - 从数据库检索数据并存储到数组时出现数据库锁定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆