Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android sqlite空指针错误_Android_Database_Sqlite_Nullpointerexception - Fatal编程技术网

android sqlite空指针错误

android sqlite空指针错误,android,database,sqlite,nullpointerexception,Android,Database,Sqlite,Nullpointerexception,我正在从事一个需要数据库的android项目。我遇到的问题是,当我启动它时,会出现nullpointerexception错误。在浏览logcat时,我将其范围缩小到数据库更新和尝试打开时。这是正常的还是我看起来有点过头了? 日志: OnUpgrade代码: public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { Log.d("MonkeyDatabase","DbUpdater");

我正在从事一个需要数据库的android项目。我遇到的问题是,当我启动它时,会出现nullpointerexception错误。在浏览logcat时,我将其范围缩小到数据库更新和尝试打开时。这是正常的还是我看起来有点过头了?
日志:

OnUpgrade代码:

public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) 
       {
        Log.d("MonkeyDatabase","DbUpdater");
        db.execSQL("Drop table if exists "+TABLE_SITE_NAME);
        db.execSQL("Drop table if exists "+TABLE_UNIT_NAME);
        db.execSQL("Drop table if exists "+TABLE_BONESHELL_NAME);
        db.execSQL("Drop table if exists "+TABLE_CERAMIC_NAME);
        db.execSQL("Drop table if exists "+TABLE_LITHIC_NAME);
        onCreate(db);
        }
开放代码:

public BmDb open() throws SQLException
{
    Log.d("Database","open");
    db= BmDb.getWritableDatabase();
    return this;
}
更多代码:

public BmDb(Context context)
{
    this.context= context;
    BmDb= new DatabaseHelper(context);
}
public static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context){
        super(context, DATABASE_NAME, null, VERSION);

}
这是ChooseSite文件中的代码(带有行号):

关于我正在查看的内容有什么建议吗?

来自此代码

 final BmDb db=new BmDb(this);
db.open(); **this is the line mentioned in my logcat**
final Cursor c= db.GetSite();
if (c.moveToFirst()){
    do{
        sites(c);
    }while (c.moveToNext());
}
调用此方法两次db.open()将此设置为try{}catch{}black

如下所示,从这段代码中尝试{db.open();db.open();}catch(){}

 final BmDb db=new BmDb(this);
db.open(); **this is the line mentioned in my logcat**
final Cursor c= db.GetSite();
if (c.moveToFirst()){
    do{
        sites(c);
    }while (c.moveToNext());
}
调用此方法两次db.open()将此设置为try{}catch{}black


如下所示,在您的
onUpgrade中尝试{db.open();db.open();}catch(){}

public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) 
       {
        Log.d("MonkeyDatabase","DbUpdater");
        db.execSQL("Drop table if exists "+TABLE_SITE_NAME);
        db.execSQL("Drop table if exists "+TABLE_UNIT_NAME);
        db.execSQL("Drop table if exists "+TABLE_BONESHELL_NAME);
        db.execSQL("Drop table if exists "+TABLE_CERAMIC_NAME);
        db.execSQL("Drop table if exists "+TABLE_LITHIC_NAME);
         //onCreate(db);
         onCreate(arg0);
        }

如果应用程序代码中增加了数据库版本,则调用
onUpgrade()
。检查您的代码。

在您的
onUpgrade

public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) 
       {
        Log.d("MonkeyDatabase","DbUpdater");
        db.execSQL("Drop table if exists "+TABLE_SITE_NAME);
        db.execSQL("Drop table if exists "+TABLE_UNIT_NAME);
        db.execSQL("Drop table if exists "+TABLE_BONESHELL_NAME);
        db.execSQL("Drop table if exists "+TABLE_CERAMIC_NAME);
        db.execSQL("Drop table if exists "+TABLE_LITHIC_NAME);
         //onCreate(db);
         onCreate(arg0);
        }

如果应用程序代码中增加了数据库版本,则调用
onUpgrade()
。检查您的代码。

为什么这有助于更正它?它起作用了,但只是遇到了另一个错误,那就是我的GetSite代码:public Cursor GetSite(){return db.query(TABLE_SITE_NAME,new String[]{KEY_SITEID,KEY_SITENAME,KEY_SITELOC},null,null,null,null);}我不熟悉将SQLite与android一起使用,但从我找到的所有教程来看,这是常见的形式。我把它标错了吗?看看android开发者网站,我发现了两个游标查询实例。一个具有8个参数,另一个具有7个参数。我使用的是7个参数,如下所示:公共游标查询(SQLiteDatabase db,String[]projectionIn,String selection,String[]selectionArgs,String groupBy,String having,String sortOrder),据我所知,我是正确的。有什么我错过的吗?好吧,我觉得自己很愚蠢。为什么try{}catch(){}语句会产生如此大的差异?这也解决了我刚才评论的另一个问题。我还发现,我只需要在try{}catch{}中调用db.open()一次,为什么这有助于纠正它呢?它起作用了,但只是遇到了另一个错误,那就是我的GetSite代码:public Cursor GetSite(){return db.query(TABLE_SITE_NAME,new String[]{KEY_SITEID,KEY_SITENAME,KEY_SITELOC},null,null,null,null);}我不熟悉将SQLite与android一起使用,但从我找到的所有教程来看,这是常见的形式。我把它标错了吗?看看android开发者网站,我发现了两个游标查询实例。一个具有8个参数,另一个具有7个参数。我使用的是7个参数,如下所示:公共游标查询(SQLiteDatabase db,String[]projectionIn,String selection,String[]selectionArgs,String groupBy,String having,String sortOrder),据我所知,我是正确的。有什么我错过的吗?好吧,我觉得自己很愚蠢。为什么try{}catch(){}语句会产生如此大的差异?这也解决了我刚才评论的另一个问题。我还发现,我只需要在try{}catch{}中调用db.open()一次,要升级的代码是在实际程序中标记的,还是在我的数据库文件的顶部;我称之为哪个版本?要升级的代码,是在实际程序中标记的,还是在顶部的数据库文件中标记的;我称之为版本?