我得到的错误是
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory
相关代码 -
public void delete(int position) {
SQLiteDatabase db = getWritableDatabase(); //this is null
db.execSQL("DELETE FROM LOGIN WHERE _ID=1;");
db.close();
}
这是DBHelper.class中的代码。数据添加和选择有效但删除仅导致错误。这有什么不对?
所有DBHelper代码
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import static android.content.ContentValues.TAG;
public class DBHelper extends SQLiteOpenHelper {
private Context context;
private DBHelper dbHelper;
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
this.context = context;
}
@Override public void onCreate(SQLiteDatabase db) {
try {
String DROP_SQL = "drop table if exists LOGIN";
db.execSQL(DROP_SQL);
} catch(Exception ex) {
Log.e(TAG, "Exception in DROP_SQL", ex);
}
StringBuffer sb = new StringBuffer();
sb.append(" CREATE TABLE LOGIN ( ");
sb.append(" _ID INTEGER PRIMARY KEY AUTOINCREMENT, ");
sb.append(" ID TEXT, ");
sb.append(" PW TEXT ) ");
try {
db.execSQL(sb.toString());
} catch(Exception ex) {
Log.e(TAG, "Exception in CREATE_SQL", ex);
}
Toast.makeText(context, R.string.make, Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Toast.makeText(context, R.string.upgrade, Toast.LENGTH_SHORT).show();
}
public void addLogin(Login login) {
SQLiteDatabase db = getWritableDatabase();
StringBuffer sb = new StringBuffer();
sb.append(" INSERT INTO LOGIN ( ");
sb.append(" ID, PW ) ");
sb.append(" VALUES ( ?, ? ) ");
if(login.getId().equals("") || login.getPw().equals("")){
Toast.makeText(context, R.string.nodata, Toast.LENGTH_SHORT).show();
}else{
db.execSQL(sb.toString(),
new Object[]{
login.getId(),
login.getPw(),
});;
Toast.makeText(context, R.string.insert, Toast.LENGTH_SHORT).show();
}
}
public void delete(int position) {
SQLiteDatabase db = getWritableDatabase(); //this is null
db.execSQL("DELETE FROM LOGIN WHERE _ID=1;");
db.close();
//????
//db.execSQL("DELETE FROM LOGIN WHERE _ID=" + position + ";");
//?? ???
}
public List getAllData() {
StringBuffer sb = new StringBuffer();
sb.append(" SELECT _ID, ID, PW FROM LOGIN ");
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(sb.toString(), null);
List list = new ArrayList();
Login login = null;
while (cursor.moveToNext()) {
login = new Login();
login.set_id(cursor.getInt(0));
login.setId(cursor.getString(1));
login.setPw(cursor.getString(2));
list.add(login);
}
return list;
}
}
完整错误
08-01 11:14:34.094 23589-23589 / com.yonoo.naverloginhelper D / ViewRootImpl:ViewPostImeInputStage processPointer 0 08-01 11:14:34.134 23589-23589 / com.yonoo.naverloginhelper D / ViewRootImpl:ViewPostImeInputStage processPointer 1 08-01 11:24:33.866 23589-23589 / com.yonoo.naverloginhelper W / IInputConnectionWrapper:getTextBeforeCursor on inactive InputConnection 08-01 11:24:33.916 23589-23589 / com.yonoo.naverloginhelper V / ActivityThread:updateVisibility:ActivityRecord {a6dc7d0 token=android.os.BinderProxy@5f1945b {com.yonoo.naverloginhelper / com.yonoo.naverloginhelper.DBActivity} } show:true 08-01 11:24:33.916 23589-23589 / com.yonoo.naverloginhelper W / IInputConnectionWrapper:getSelectedText在非活动的InputConnection上 08-01 11:24:33.916 23589-23589 / com.yonoo.naverloginhelper W / IInputConnectionWrapper:getTextAfterCursor on inactive InputConnection 08-01 11:24:39.086 23589-23589 / com.yonoo.naverloginhelper D / ViewRootImpl:MSG_RESIZED_REPORT:ci = Rect(0,96 - 0,0)vi = Rect(0,96 - 0,0)或= 1 08-01 13:36:26.303 23589-23589 / com.yonoo.naverloginhelper D / ViewRootImpl:MSG_RESIZED:ci = Rect(0,96-0,1000)vi = Rect(0,96-0,1000)或= 1 08-01 13:36:26.373 23589-23589 / com.yonoo.naverloginhelper I / Timeline:Timeline:Activity_idle id:android.os.BinderProxy@5f1945b time:11500752 08-01 13:36:27.323 23589-23589 / com.yonoo.naverloginhelper D / ViewRootImpl:ViewPostImeInputStage processPointer 0 08-01 13:36:27.463 23589-23589 / com.yonoo.naverloginhelper D / ViewRootImpl:ViewPostImeInputStage processPointer 1 08-01 13:36:27.963 23589-23589 / com.yonoo.naverloginhelper D / ViewRootImpl:ViewPostImeInputStage processPointer 0 08-01 13:36:28.063 23589-23589 / com.yonoo.naverloginhelper D / ViewRootImpl:ViewPostImeInputStage processPointer 1 08-01 13:36:28.073 23589-23589 / com.yonoo.naverloginhelper D / AndroidRuntime:关闭虚拟机
java.lang.NullPointerException:尝试调用虚方法' android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String,int,android.database.sqlite.SQLiteDatabase $ CursorFactory ,android.database.DatabaseErrorHandler)'在null对象引用上 在android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:283) 在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223) 在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) 在com.yonoo.naverloginhelper.DBHelper.delete(DBHelper.java:71) 在com.yonoo.naverloginhelper.DBActivity.DeleteList(DBActivity.java:70) 在com.yonoo.naverloginhelper.LoginListAdapter $ 1.onClick(LoginListAdapter.java:76) 在android.view.View.performClick(View.java:5702) 在android.widget.TextView.performClick(TextView.java:10888) 在android.view.View $ PerformClick.run(View.java:22541) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:158) 在android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
检查您的应用权限。清单和运行时。
您在哪里设置数据库名称和版本?
您可以像这样创建构造函数。您的数据库名称和版本始终相同。
// Database version
private static final int DATABASE_VERSION = 1;
// Database name
private static final String DATABASE_NAME = MyDatabase.db;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}