Android - Load Cursor from SQLite -


i want load 3 columns sqlite db inside fragment. fragment should:

  • load db (db full, copy later use)
  • create cursor
  • send object data custom listadapter

i error:

attempt invoke virtual method 'android.database.cursor android.database.sqlite.sqlitedatabase.query(java.lang.string, java.lang.string[], java.lang.string, java.lang.string[], java.lang.string, java.lang.string, java.lang.string)' on null object reference @ com.example.somap.myapp.listfragment.loadmonsterlinklist(listfragment.java:88)

i think problem initial database load. dont see reason why loaded db = null

fragment activity (cut):

public class listfragment extends fragment {  private onfragmentinteractionlistener mlistener;  /** ----- global variables ----- **/ /** log **/ private static string tag = "monster list"; /** view changes **/ private view myview = null; /** db query **/ private string categoryid = null; /** db **/ private sqlitedatabase mydatabase = null;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate); }  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     //load view     this.myview = inflater.inflate(r.layout.fragment_list, container,false);      //change category label     setcategorylabel(); --> ok      //load db;     loaddatabase();      //create list monsterlist     monsterlink[] monsterlinklist = loadmonsterlinklist();       .     . //some code     .     .      // inflate layout fragment     return this.myview; //inflater.inflate(r.layout.fragment_list, container, false); }  public monsterlink[] loadmonsterlinklist() {     log.i(this.tag, "loadmonsterlinklist");      //query     string table = "monster";     string[] columns = {"id", "displayname", "imagepath"};     string = "category=\"" + this.categoryid + "\"";     cursor cursor = this.mydatabase.query(table,columns,where,null,null,null,null); => here error      monsterlink[] list = new monsterlink[cursor.getcount()];      int = 0;     while (cursor.movetonext()){         log.i(this.tag, cursor.getstring(0) + "  " + cursor.getstring(1) + "  " + cursor.getstring(2));         monsterlink monsterlink = new monsterlink(cursor.getstring(0),cursor.getstring(1),cursor.getstring(2));         list[i] = monsterlink;         i++;     }      return list; }  protected void loaddatabase(){      mydbhelper mydb = new mydbhelper(this.myview.getcontext());      try {          mydb.createdatabase();         this.mydatabase =  mydb.getdatabase(); => mydatabase null, why?      } catch (ioexception ioe) {          throw new error("unable create database");      } } 

here mydbhelper class (i working fine in main activity):

public class mydbhelper extends sqliteopenhelper {  //the androids default system path of application database. private static string db_path = "/data/data/com.example.somap.myapp/databases/";  private static string db_name = "bestiary.db";  private sqlitedatabase mydatabase;  private final context mycontext;  public sqlitedatabase getdatabase(){return this.mydatabase;}  /**  * constructor  * takes , keeps reference of passed context in order access application assets , resources.  * @param context  */ public mydbhelper(context context) {      super(context, db_name, null, 1);     this.mycontext = context; } /**  * creates empty database on system , rewrites own database.  * */ public void createdatabase() throws ioexception {      boolean dbexist = checkdatabase();      if(dbexist){         //do nothing - database exist     }else{         //by calling method , empty database created default system path         //of application gonna able overwrite database our database.         this.getreadabledatabase();          try {              copydatabase();          } catch (ioexception e) {              throw new error("error copying database");          }     }  }  /**  * check if database exist avoid re-copying file each time open application.  * @return true if exists, false if doesn't  */ private boolean checkdatabase(){      sqlitedatabase checkdb = null;      try{         string mypath = db_path + db_name;         checkdb = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly);      }catch(sqliteexception e){         //database does't exist yet.     }      if(checkdb != null){         checkdb.close();     }      return checkdb != null ? true : false; }  /**  * copies database local assets-folder created empty database in  * system folder, can accessed , handled.  * done transfering bytestream.  * */ private void copydatabase() throws ioexception{      //open local db input stream     inputstream myinput = mycontext.getassets().open(db_name);      // path created empty db     string outfilename = db_path + db_name;      //open empty db output stream     outputstream myoutput = new fileoutputstream(outfilename);      //transfer bytes inputfile outputfile     byte[] buffer = new byte[1024];     int length;     while ((length = myinput.read(buffer))>0){         myoutput.write(buffer, 0, length);     }      //close streams     myoutput.flush();     myoutput.close();     myinput.close();  }  public void opendatabase() throws sqlexception {      //open database     string mypath = db_path + db_name;     mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly);  }  @override public synchronized void close() {      if(mydatabase != null)         mydatabase.close();      super.close();  } 

i think should transfer db functions in onactivitycreated or onviewcreated. before getcontext() activity null.


Comments