java - Populate ListView From SQLite database in onCreate() in Android -


i created activity 'a' , 'b'.i have inserted data in activity 'a' , want display data in listview in activity 'b' on button click in activity 'a'.but when run application crashes , getting error , nullpointerexception.please me.thanks in advance.

here database method code.

public cursor employeename(){             arraylist<employee> employeenamelist = new arraylist<employee>();             string selectquery = "select * " + employee_details_table;             sqlitedatabase db = this.getwritabledatabase();             cursor cursor = db.rawquery(selectquery, null);             if(cursor.movetofirst())             {                 do{                      employee empname = new employee();                     empname.setname(cursor.getstring(1));                     empname.setdepartment(cursor.getstring(2));                     empname.setdesignation(cursor.getstring(3));                       employeenamelist.add(empname);                 } while(cursor.movetonext());             }             return cursor;         }             public list<string> getallemployeename() {             list<string> labels = new arraylist<string>();                 // select query                string selectquery = "select  * " + employee_details_table;                 sqlitedatabase db = this.getreadabledatabase();                cursor cursor = db.rawquery(selectquery, null);                if (cursor.movetofirst()) {                    {                     labels.add(cursor.getstring(1));                    } while (cursor.movetonext());                }                cursor.close();                db.close();             return labels;           } 

here activity code

public class employee_list extends listactivity {     databasehelper databasehelper;     context context;     employee emp;      protected sqlitedatabase db;     protected cursor cursor;     protected listadapter adapter;       public void oncreate(bundle savedinstancestate)     {         super.oncreate(savedinstancestate);         setcontentview(r.layout.staff_employee_list);         populatelistview();     }      private void populatelistview()      {         databasehelper databasehelper = new databasehelper(context);         db = (new databasehelper(this)).getwritabledatabase();         cursor cursor =(cursor) databasehelper.employeename();         string[] fromfieldnames = new string[]         {databasehelper.staff_employee_name,databasehelper.department,databasehelper.designation};         int[] toviewids = new int[]         {r.id.textviewempname, r.id.textviewdepartmet, r.id.textviewdesignation};         simplecursoradapter mycursoradapter;         mycursoradapter = new simplecursoradapter(getbasecontext(), r.layout.staff_employee_list_item, cursor, fromfieldnames, toviewids);         listview mylist = (listview) findviewbyid(android.r.id.list);         mylist.setadapter(mycursoradapter);         }   } 

here logcat

06-27 15:25:51.498: e/androidruntime(12033): fatal exception: main 06-27 15:25:51.498: e/androidruntime(12033): java.lang.runtimeexception: unable start activity componentinfo{com.employee_review/com.employee_review.employee_list}: java.lang.nullpointerexception 06-27 15:25:51.498: e/androidruntime(12033):    @ android.app.activitythread.performlaunchactivity(activitythread.java:1647) 06-27 15:25:51.498: e/androidruntime(12033):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 06-27 15:25:51.498: e/androidruntime(12033):    @ android.app.activitythread.access$1500(activitythread.java:117) 06-27 15:25:51.498: e/androidruntime(12033):    @ android.app.activitythread$h.handlemessage(activitythread.java:931) 06-27 15:25:51.498: e/androidruntime(12033):    @ android.os.handler.dispatchmessage(handler.java:99) 06-27 15:25:51.498: e/androidruntime(12033):    @ android.os.looper.loop(looper.java:123) 06-27 15:25:51.498: e/androidruntime(12033):    @ android.app.activitythread.main(activitythread.java:3683) 06-27 15:25:51.498: e/androidruntime(12033):    @ java.lang.reflect.method.invokenative(native method) 06-27 15:25:51.498: e/androidruntime(12033):    @ java.lang.reflect.method.invoke(method.java:507) 06-27 15:25:51.498: e/androidruntime(12033):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 06-27 15:25:51.498: e/androidruntime(12033):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 06-27 15:25:51.498: e/androidruntime(12033):    @ dalvik.system.nativestart.main(native method) 06-27 15:25:51.498: e/androidruntime(12033): caused by: java.lang.nullpointerexception 06-27 15:25:51.498: e/androidruntime(12033):    @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:118) 06-27 15:25:51.498: e/androidruntime(12033):    @ com.employee_review.databasehelper.employeename(databasehelper.java:129) 06-27 15:25:51.498: e/androidruntime(12033):    @ com.employee_review.employee_list.populatelistview(employee_list.java:35) 06-27 15:25:51.498: e/androidruntime(12033):    @ com.employee_review.employee_list.oncreate(employee_list.java:28) 06-27 15:25:51.498: e/androidruntime(12033):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 06-27 15:25:51.498: e/androidruntime(12033):    @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 06-27 15:25:51.498: e/androidruntime(12033):    ... 11 more 

the context pass database helper constructor here not initialized:

databasehelper databasehelper = new databasehelper(context); 

replace context e.g. this.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -