简体   繁体   中英

NullPointerException while accessing SQLite from non-activity class

While accessing SQLite from non-activity class , getting NullPointerException

File -> 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
}
}

File -> 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
    }

}

Then, what should be the best way to do this?

You don't seem to be calling SessionDB.open() anywhere. This means that your database field is never being initialized, so is null when you call SessionDB.createSession().

I added one line to the topic starter's code. Now it should work. Check it please.

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
    }
}

File -> SessionDB.java should extend SQLiteOpenHelper

i have modified the code

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

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

}

on the class after initialized Sqlite Database you most calling open function. for example:

Database database = new Database();
database.open(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM