【发布时间】:2013-05-11 14:28:55
【问题描述】:
从非活动类访问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
}
}
那么,最好的方法应该是什么?
【问题讨论】:
标签: java android nullpointerexception null-pointer