0

非活动类访问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
    }

}

那么,最好的方法应该是什么?

4

4 回答 4

0

File -> 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();
    }

}
于 2013-05-14T07:26:22.577 回答
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
    }
}
于 2013-05-13T18:57:43.103 回答
0

您似乎没有在任何地方调用 SessionDB.open() 。这意味着您的数据库字段永远不会被初始化,因此当您调用 SessionDB.createSession() 时为 null。

于 2013-05-11T14:31:25.720 回答
0

在初始化 Sqlite Database 之后的类上你最常调用 open 函数。例如:

Database database = new Database();
database.open(); 
于 2017-09-18T06:13:13.130 回答