每当我尝试向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中插入记录
public class 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文件并且数据库存在,但数据库变量始终为null
答案 0 :(得分: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;
}
对于您的信息,您必须在任何查询之前和之后打开和关闭数据库