从非活动类访问 SQLite 时,获取NullPointerException
档案 - > SessionDB.java
public class SessionDB {
private SQLiteDatabase database;
private SQLiteHelper dbHelper;
private String[] allColumns = { SQLiteHelper.SESSION_ID,
SQLiteHelper.SESSION_STRING };
public SessionDB(Context context) {
dbHelper = new SQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void createSession(String session_string) {
ContentValues values = new ContentValues();
values.put(SQLiteHelper.SESSION_STRING, session_string);
database.insert(SQLiteHelper.SESSION_TABLE, null, values); // NullPointerException Here
}
}
文件 - > Live.java
public class Live {
protected Context context;
public Live(Context context) {
this.context = context;
}
public accessOK {
SessionDB ses = new SessionDB(context);
ses.createSession("Sample"); // NullPointerException
}
}
然后,最好的方法是什么?
答案 0 :(得分:0)
您似乎没有在任何地方调用SessionDB.open()。这意味着您的数据库字段永远不会被初始化,因此在调用SessionDB.createSession()时为null。
答案 1 :(得分:0)
我在主题入门代码中添加了一行。现在它应该工作。请检查一下。
public class Live {
protected Context context;
public Live(Context context) {
this.context = context;
}
public accessOK {
SessionDB ses = new SessionDB(context);
// You have missed call to open method here:
ses.open();
ses.createSession("Sample"); // NullPointerException
}
}
答案 2 :(得分:0)
档案 - > SessionDB.java应该扩展SQLiteOpenHelper
我修改了代码
public class SessionDB extends SQLiteOpenHelper {
public static final String TABLE_NAME = "SESSION_TABLE";
public static final String COLUMN_1 = "SESSION_ID";
public static final String COLUMN_2 = "SESSION_STRING";
private static final String DATABASE_NAME = "session.db";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase database;
// Database creation sql statement
private static final String CREATE_TABLE = "create table "
+ TABLE_NAME + "(" + COLUMN_1 + " integer primary key autoincrement," + COLUMN_2
+ " text);";
public DataBase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
/*
* Log.w(MySQLiteHelper.class.getName(),
* "Upgrading database from version " + oldVersion + " to " + newVersion
* + ", which will destroy all old data");
*/
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void open() throws SQLException {
database = this.getWritableDatabase();
}
public void close() {
database.close();
}
public void createSession(String session_string) {
ContentValues values = new ContentValues();
values.put(COLUMN_2, session_string);
database.insert(TABLE_NAME, null, values);
}
}
文件 - > Live.java
public class Live {
protected Context context;
public Live(Context context) {
this.context = context;
}
public accessOK {
SessionDB ses = new SessionDB(context);
ses.open();
ses.createSession("Sample");
ses.close();
}
}
答案 3 :(得分:0)
在初始化Sqlite数据库之后,你最多调用open函数。 例如:
Database database = new Database();
database.open();