android - unable to get data save in database in fragment containing recyclerview -
i'm working on integrating database save & contact information retrieved in fragment contain recyclerview. app directly crashing when started.
fatal exception: main process: com.example.admin.navigationdrawer_demo, pid: 15637 java.lang.runtimeexception: unable start activity componentinfo{com.example.admin.navigationdrawer_demo/com.example.admin.navigationdrawer_demo.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.database.sqlite.sqlitedatabase android.content.context.openorcreatedatabase(java.lang.string, int, android.database.sqlite.sqlitedatabase$cursorfactory, android.database.databaseerrorhandler)' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2416) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) @ android.app.activitythread.-wrap11(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1344) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.database.sqlite.sqlitedatabase android.content.context.openorcreatedatabase(java.lang.string, int, android.database.sqlite.sqlitedatabase$cursorfactory, android.database.databaseerrorhandler)' on null object reference @ android.database.sqlite.sqliteopenhelper.getdatabaselocked(sqliteopenhelper.java:223) @ android.database.sqlite.sqliteopenhelper.getreadabledatabase(sqliteopenhelper.java:187) @ com.example.admin.navigationdrawer_demo.person_db_openhelper.getallperson_mycontact(person_db_openhelper.java:149) @ com.example.admin.navigationdrawer_demo.homefragment.filllistbirthdays(homefragment.java:64) @ com.example.admin.navigationdrawer_demo.homefragment.oncreateview(homefragment.java:55) @ android.support.v4.app.fragment.performcreateview(fragment.java:1974) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1067) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1252) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:742) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1617) @ android.support.v4.app.fragmentcontroller.execpendingactions(fragmentcontroller.java:339) @ android.support.v4.app.fragmentactivity.onstart(fragmentactivity.java:601) @ android.app.instrumentation.callactivityonstart(instrumentation.java:1237) @ android.app.activity.performstart(activity.java:6253) @ android.app.activitythread.performlaunchactivity(activitythread.java:2379) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476)
i able create database
databasehelper.java:
public class person_db_openhelper extends sqliteopenhelper { string tag="database"; public static string db_name="community_image.db"; public static int db_version=1; public static string table_name="person_detail_image"; public static string col_id="_id"; private static final string col_key_image = "image"; public static string col_first_name="first_name"; public static string col_middle_name="middle_name"; public static string col_last_name="last_name"; public static string col_dob_string="date_of_birth_string"; public static string col_date="date"; public static string col_month="month"; public static string col_email_id_mandatory="email_id_mandatory"; public static string col_email_id="email_id"; public static string col_postal_address="postal_address"; public static string col_office_address="office_address"; public static string col_messanger_address="messanger_address"; private string create_table= "create table "+table_name+" ( "+ col_id+" integer primary key autoincrement, "+ col_key_image+" blob, "+ col_first_name+" text, "+ col_middle_name+" text, "+ col_last_name+" text, "+ col_dob_string+" text, "+ col_date+" long, "+ col_month+" long, "+ col_email_id+" text, "+ col_postal_address+" text, "+ col_office_address+" text, "+ col_messanger_address+" text);"; public person_db_openhelper(context context) { super(context,db_name,null,db_version); } @override public void oncreate(sqlitedatabase db) { log.d("create","table"); db.execsql(create_table); } @override public void onupgrade(sqlitedatabase db, int i, int i1) { db.execsql("drop table if exists " + table_name); oncreate(db); } public long insertperson(person person) { sqlitedatabase database=getwritabledatabase(); contentvalues contentvalues=new contentvalues(); contentvalues.put(col_key_image,person.get_image()); contentvalues.put(col_first_name,person.getfirst_name()); contentvalues.put(col_middle_name,person.getmiddle_name()); contentvalues.put(col_last_name,person.getlast_name()); contentvalues.put(col_dob_string,person.getdob()); contentvalues.put(col_date,person.getdate1()); contentvalues.put(col_month,person.getmonth()); contentvalues.put(col_year,person.getyear()); contentvalues.put(col_doa,person.getdoa()); contentvalues.put(col_member_type,person.getmember_type()); contentvalues.put(col_phone_mandatory,person.getphone_mandatory()); contentvalues.put(col_phone,person.getphone()); contentvalues.put(col_land_line_number,person.getland_line_number()); contentvalues.put(col_email_id_mandatory,person.getemail_id_mandatory()); contentvalues.put(col_email_id,person.getemail_id()); contentvalues.put(col_postal_address,person.getpostal_address()); contentvalues.put(col_office_address,person.getoffice_address()); contentvalues.put(col_messanger_address,person.getmessanger_address()); log.d("object:","person"+person); long inserted_record_row_id=database.insert(table_name,null,contentvalues); return inserted_record_row_id; } // public cursor getallperson_mycontact() public list<person> getallperson_mycontact() { list <person> listmycontacts=new arraylist<>(); sqlitedatabase database=getreadabledatabase(); cursor cursor_all_record=database.query(table_name,new string[]{col_id,col_first_name,col_middle_name,col_last_name,col_phone_mandatory},null,null,null,null,null); //cursor cursor_all_record=database.query(table_name,new string[]{col_middle_name},null,null,null,null,null,null); log.d(tag,"cursor "+cursor_all_record.getcount()); while (cursor_all_record.movetonext()) { person p=new person(); string name=cursor_all_record.getstring(1); p.setfirst_name(name); // log.d("database", "name:- "+name); listmycontacts.add(p); } //return cursor_all_record; return listmycontacts; } @targetapi(build.version_codes.n)
homefragment.java:
public class homefragment extends fragment { private context context; // context context=getcontext(); person_db_openhelper person_db_openhelper=new person_db_openhelper(context); // event_db_openhelper event_db_openhelper=new event_db_openhelper(context); private list<person> birthdays=new arraylist<>(); private list<event> events=new arraylist<>(); private list<person> aniversaries=new arraylist<>(); public homefragment() { } public static homefragment newinstance() { homefragment fragment = new homefragment(); return fragment; } public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_main, container, false); //context context = view.getcontext(); recyclerview recyclerview = (recyclerview) view; filllistevents(); filllistbirthdays(); filllistaniversaries(); recyclerview.setlayoutmanager(new linearlayoutmanager(context)); recyclerview.setadapter(new homeadapter(birthdays,aniversaries,events)); return view; } private void filllistbirthdays() { birthdays=person_db_openhelper.getallperson_mycontact(); /* birthday birthday; (int = 0; < 2; i++) { birthday = new birthday("birthday " + (i + 1)); birthdays.add(birthday); }*/ }
Comments
Post a Comment