android - NullPointerException sqlite -


i trying store oauth tokens in database using sqlite. when creating new row, getting "nullpointerexception." receiving token instagram (it works if storing in shared prefs), fails if try , store in sqlite.

my tokendatasource:

public class tokendatasource  {       // database fields       private sqlitedatabase database;       private dbhelper dbhelper;       private string[] allcolumns = { dbhelper.column_id,           dbhelper.column_token };        public void close(){            dbhelper.close();       }        public void open() throws sqlexception{            database = dbhelper.getwritabledatabase();       }        public tokendatasource(context context){            dbhelper = new dbhelper(context);       }        public void deleteinstagramtoken(instagramtoken token)       {           long id = token.getid();           database.delete(dbhelper.table_insta_tokens, dbhelper.column_id                     + " = " + id, null);       }        public instagramtoken createinstagramtoken(string token)        {             contentvalues values = new contentvalues();             values.put(dbhelper.column_token, token);             long insertid = database.insert(dbhelper.table_insta_tokens, null,                 values);             cursor cursor = database.query(dbhelper.table_insta_tokens,                 allcolumns, dbhelper.column_id + " = " + insertid, null,                 null, null, null);             cursor.movetofirst();             instagramtoken newtoken = cursortotoken(cursor);             cursor.close();              return newtoken;       }        public list<instagramtoken> getallinstagramtokens()       {           list<instagramtoken> tokens = new arraylist<instagramtoken>();            cursor cursor = database.query(dbhelper.table_insta_tokens,                     allcolumns, null, null, null, null, null);           cursor.movetofirst();            while (!cursor.isafterlast())           {               instagramtoken token = cursortotoken(cursor);               tokens.add(token);               cursor.movetonext();           }            cursor.close();            return tokens;       }        private instagramtoken cursortotoken(cursor cursor)       {           instagramtoken token = new instagramtoken();            token.setid(cursor.getlong(0));           token.settoken(cursor.getstring(1));            return token;       } } 

my dbhelper:

public class dbhelper extends sqliteopenhelper  {       public static final string table_insta_tokens = "insta_tokens";       public static final string column_id = "_id";       public static final string column_token = "token";        private static final string database_name = "status.db";       private static final int database_version = 1;        // database creation sql statement       private static final string database_create = "create table "           + table_insta_tokens + "(" + column_id           + " integer primary key autoincrement, " + column_token           + " text not null);";        public dbhelper(context context) {         super(context, database_name, null, database_version);       }        @override       public void oncreate(sqlitedatabase database) {         database.execsql(database_create);       }        @override       public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         log.w(dbhelper.class.getname(),             "upgrading database version " + oldversion + " "                 + newversion + ", destroy old data");         db.execsql("drop table if exists " + table_insta_tokens);         oncreate(db);       } } 

the storeaccesstoken method:

public instagramsession(context context)  {     this.mctx = context;     sharedpref = context.getsharedpreferences(shared, context.mode_private);     editor = sharedpref.edit(); }  public void storeaccesstoken(string accesstoken, string id, string username, string name)  {     tokendatasource datasource = new tokendatasource(mctx);      editor.putstring(api_id, id);     editor.putstring(api_name, name);     editor.putstring(api_access_token, accesstoken);     editor.putstring(api_username, username);     editor.commit();      datasource.createinstagramtoken(accesstoken); } 

instead of have method stores in member class instance of sqlitedatabase should call dbhelper.getwritabledatabase() every time need access


Comments

Popular posts from this blog

Change the color of an oval at click in Java AWT -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -