【问题标题】:SQLite NullPointerException when calling insert()调用 insert() 时的 SQLite NullPointerException
【发布时间】:2014-12-28 19:25:36
【问题描述】:

每当我尝试将新记录插入 db 时,我都会收到 NullPointerException 不知道是创建还是打开问题 这是我的 SQLiteHelper 类:

public MySQLiteHelper(Context context){
    super(context, DB_NAME, null, DATABASE_VERSION);
 }
private static final String DB_CREATE_QUERY   = "CREATE TABLE "+TAB_NAME+" (" +
        ""+COL_ID+" integer PRIMARY KEY autoincrement," +
        ""+COL_xxxLE+" LONG," +
        ""+COL_UxxxILE+" LONG," +
        ""+COL_xxxFI+" LONG," +
        ""+COL_xxxxIFI+" LONG,"+
        ""+COL_DATE+" DATETIME  );";

private static final String DB_CREATE_QUERY_TEMP   = "CREATE TABLE "+TAB_NAME_TEMP+" (" +
        ""+COL_ID+" integer PRIMARY KEY autoincrement," +
        ""+COLxxILE+" LONG," +
        ""+COL_xxILE+" LONG," +
        ""+COL_xxFI+" LONG," +
        ""+COLxxxWIFI+" LONG,"+
        ""+COLxxxDATE+" DATETIME ) ;";

@Override
public void onCreate(SQLiteDatabase database){
    try {
        database.execSQL(DB_CREATE_QUERY);
        database.execSQL(DB_CREATE_QUERY_TEMP);
    } catch (SQLiteException exc)
    {
        Log.e("SQL",exc.toString());
    }
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    Log.w("TAHA->","Upgrading database from version: "+oldVersion+" to "+newVersion+". This will destroy all existing data");
    db.execSQL("DROP TABLE IF EXISTS "+ TAB_NAME);
    db.execSQL("DROP TABLE IF EXISTS "+ TAB_NAME_TEMP);
    onCreate(db);
}

这是我在其中插入记录的 RecordSource.java

公共类 RecordSource {

private MySQLiteHelper dbHelper;
private SQLiteDatabase database;

private String[] myColumns =
        {
                MySQLiteHelper.xxxLE,
                MySQLiteHelper.xxxxILE,
                MySQLiteHelper.xxxxFI,
                MySQLiteHelper.xxxxIFI,
};
/**
 *CONSTRUCTOR
 * calling MySQLiteHelper class which maintains my DB each time
 * to make sure my DB is ready
 */
public RecordSource(Context context)
{
    dbHelper = new MySQLiteHelper(context);
}



/**
 * Open and close methods, used to free memory
 * @throws SQLiteException
 */
public void open() throws SQLiteException
{
    database = dbHelper.getWritableDatabase();

}

public void close()
{
    dbHelper.close();
}


public boolean createRecord(boolean firstLaunch)
{
    if(firstLaunch)
        freshStart();
    else {
        Record rec = new Record( getLastRecord("temp_record"), true);
        insertRecord(rec, MySQLiteHelper.TAB_NAME);
        Log.w("taha->", "another record inserted");
    }
    return true;
}

public void freshStart()
{
    Record firstRecord = new Record();
    insertRecord(firstRecord, MySQLiteHelper.TAB_NAME_TEMP);
    Record rec = new Record( getLastRecord("temp_record"), true);
    insertRecord(rec,MySQLiteHelper.TAB_NAME);
    Log.w("taha->", "fresh record inserted");
}
public boolean insertRecord(Record rec,String tab)
{
    ContentValues rec_values = new ContentValues();
    rec_values.put(myColumns[0],rec.getDown_mobile());
    rec_values.put(myColumns[1],rec.getUp_mobile());
    rec_values.put(myColumns[2],rec.getDown_wifi());
    rec_values.put(myColumns[3],rec.getUp_wifi());

    try {
        database.insert(tab, null, rec_values);
    } catch (SQLiteException ex)
    {
        Log.w("taha->", ex.toString());
        return false;
    }
    return true;
}

我从 MyService 调用它

 Context ctx = getApplicationContext();

                    rec = new RecordSource(context);

这是错误

  Process: com.xx.xx.nxxr, PID: 16742
java.lang.NullPointerException
        at com.xxxr.RecordSource.insertRecord(RecordSource.java:105)
        at com.xxer.RecordSource.freshStart(RecordSource.java:83)
        at comxxxher.RecordSource.createRecord(RecordSource.java:71)
        at com.xxxer.MyService$1.run(MyService.java:43)

我检查了 db 文件,数据库存在,但数据库变量始终为空

【问题讨论】:

  • 能否给我们介绍一下 insertRecord() 方法的实现?
  • 抱歉,我编辑了我的帖子并添加了缺失的方法
  • 为什么在你的服务中有这个?记录 = 新记录源(上下文);并且您的上下文名称是 ctx?
  • 非常感谢,现在它可以工作了!

标签: android sqlite nullpointerexception


【解决方案1】:

将您的 createRecord() 方法更改为:

public boolean createRecord(boolean firstLaunch)
{
    open();
    if(firstLaunch)
        freshStart();
    else {
        Record rec = new Record( getLastRecord("temp_record"), true);
        insertRecord(rec, MySQLiteHelper.TAB_NAME);
        Log.w("taha->", "another record inserted");
    }
    close();
return true;
}

为了您的信息,您必须在任何查询之前和之后打开和关闭数据库

【讨论】:

  • 数据库中的新错误 = dbHelper.getWritableDatabase();
  • 好吧,我想我找到了。在 RecordSource 类中更改您的私有 MySQLiteHelper dbHelper;对此:私有 SQLiteOpenHelper dbHelper;
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-29
相关资源
最近更新 更多