java - Android prevent app from querying an empty database on startup -
i'm building simple todo/task app in tasks presented in recycler/card view layout , upon selection of task user taken "detailed" view of specific task. i'm testing app using genymotion emulator. problem app crashes upon startup npe.
java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string com.google.api.client.util.datetime.tostring()' on null object reference @ com.github.idclark.forgetmenot.taskadapter.onbindviewholder(taskadapter.java:35) @ com.github.idclark.forgetmenot.taskadapter.onbindviewholder(taskadapter.java:17)
although i'm not entirely sure, think because haven't entered data database yet. database first queried fragment inflated main activity.
public static class placeholderfragment extends fragment { activity mactivity; recyclerview mrecyclerview; taskadapter taskadapter; addfloatingactionbutton mfabview; checkbox mcheckbox; public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); mrecyclerview = (recyclerview) rootview.findviewbyid(r.id.cardlist); mfabview = (addfloatingactionbutton) rootview.findviewbyid(r.id.normal_plus); mfabview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent createtaskintent = new intent(v.getcontext(), editactivity.class); startactivity(createtaskintent); } }); taskadapter = new taskadapter(new tasktablecontroller(getactivity()).getalltasksforuser()); return rootview; }
my taskadapter
expects arraylist<task>
public class taskadapter extends recyclerview.adapter<taskadapter.taskviewholder> { private list<task> tasklist; onitemclicklistener mitemclicklistener; public taskadapter(list<task> tasklist) { this.tasklist = tasklist; } @override public int getitemcount() { return tasklist.size(); } @override public void onbindviewholder(taskviewholder taskviewholder, int i) { task task = tasklist.get(i); taskviewholder.mtitle.settext(task.gettitle()); taskviewholder.mupdated.settext(task.getupdated().tostring()); if (task.getstatus().equals("completed")) { taskviewholder.mstatus.setchecked(true); } else taskviewholder.mstatus.setchecked(false); } @override public taskviewholder oncreateviewholder(viewgroup viewgroup, int i) { view itemview = layoutinflater.from(viewgroup.getcontext()). inflate(r.layout.tasklist_layout, viewgroup, false); return new taskviewholder(itemview); }
the actual query database looks like
public list<task> getalltasksforuser() { final string query_all_records = "select * " + taskentry.table_name; list<task> tasklist = new arraylist<>(); sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(query_all_records, null); if (cursor.getcount() > 0) { while (cursor.movetonext()) { task task = new task(); int taskid = cursor.getcolumnindex(taskentry.column_task_id); task.setid(cursor.getstring(taskid)); task.settitle(cursor.getstring(cursor.getcolumnindex(taskentry.column_task_title))); task.setdue(new datetime(cursor.getstring(cursor.getcolumnindex(taskentry.column_task_due)))); task.setnotes(cursor.getstring(cursor.getcolumnindex(taskentry.column_task_notes))); tasklist.add(task); } } else return tasklist; db.close(); return tasklist; }
when app first installed on emulator, there no data in database, think causing npe. how handle situation if there no data fragment displays empty list (ie blank). i'm checking see if cursor.getcount()
greater 0, if it's not return empty list adapter. missing rid of npe?
the crash @ following line:
taskviewholder.mupdated.settext(task.getupdated().tostring());
task.getupdate()
returns null , app crashes. don't see call setupdated. 1 of setters that, or forgot put setter in code.
on bit unrelated note: if task list empty don't close neither cursor nor database.
Comments
Post a Comment