Error with using Sqlite database helper in Fragment in inserting data in Android -


i absolute beginner android. started learning android 8 days ago. getting error using sqlite database in fragment. please wrong code?

my database helper class

public class databasehelper extends sqliteopenhelper {      private static final int database_version = 1;     private static final string database_name = "todo.db";     private static final string table_name = "task";     private static final string column_id = "id";     private static final string column_description = "description";     private static final string column_date ="date";     private static final string column_done = "done";     private static final string create_table = "create table "+table_name+" ("+column_id+" integer primary key autoincrement,"+column_description+" text,"+     column_date+" date,"+column_done+" boolean)";     sqlitedatabase db;      public databasehelper(context context)     {         super(context,database_name,null,database_version);     }       @override     public void oncreate(sqlitedatabase db)     {         this.db = db;         db.execsql(create_table);     }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         string query = "drop table if exists "+table_name;         db.execsql(query);         this.oncreate(db);     }      public  void inserttask(task task)     {         db = getwritabledatabase();         contentvalues values = new contentvalues();         values.put(column_description,task.getdescription());         values.put(column_date,task.getdate().tostring());         values.put(column_done,boolean.false.tostring());         db.insert(table_name, null, values);         db.close();     } } 

this fragment class

public class createtaskfragment extends fragment{     private databasehelper dbhelper = new databasehelper(getactivity());     private button savebtn;     private edittext tfdescription,tfdate;     private int yy,mm,dd;     @nullable     @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {           view view = inflater.inflate(r.layout.create_task,container,false);          tfdescription = (edittext)view.findviewbyid(r.id.tf_task_description);         tfdate = (edittext)view.findviewbyid(r.id.tf_task_date);         tfdate.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 setpickerdate(v);             }         });          savebtn = (button)view.findviewbyid(r.id.btn_save_task);         savebtn.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 savetask();             }         });          return view;     }      public void setpickerdate(view v)     {         final calendar calendar = calendar.getinstance();         int yy = calendar.get(calendar.year);         int mm = calendar.get(calendar.month);         int dd = calendar.get(calendar.day_of_month);         datepickerdialog datepicker = new datepickerdialog(getactivity(), new datepickerdialog.ondatesetlistener() {             @override             public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) {                 string date = string.valueof(monthofyear+1)+"/"+string.valueof(dayofmonth)+"/"+string.valueof(year);                 tfdate.settext(date);             }         }, yy, mm, dd);         datepicker.show();     }      public void savetask()     {         string description = tfdescription.gettext().tostring();         string date = tfdate.gettext().tostring();          if(description.isempty())         {             toast.maketext(getactivity().getbasecontext(),"description required",toast.length_short).show();         }         else if(date.isempty())         {             toast.maketext(getactivity().getbasecontext(),"date required",toast.length_short).show();         }         else if(description.length()<getresources().getinteger(r.integer.min_description_length))         {             string minchar = string.valueof(getresources().getinteger(r.integer.min_description_length));             toast.maketext(getactivity().getbasecontext(),"description should minium "+minchar+" characters",toast.length_short).show();         }         else{             //check date             simpledateformat format = new simpledateformat("mm/dd/yyyy");             boolean parseok = false;             date taskdate = new date();             try{                 taskdate = format.parse(date);                 task task = new task();                 task.setdescription(description);                 task.setdate(taskdate);                 dbhelper.inserttask(task);                 parseok = true;             }             catch(parseexception e)             {                 parseok = false;             }              if(parseok)             {                 //insert task database                 toast.maketext(getactivity().getbasecontext(),"task saved",toast.length_short).show();             }             else{                 toast.maketext(getactivity().getbasecontext(),"invalid date format",toast.length_short).show();             }         }     } } 

when click save button. throwing error.

this logcat after click save button

   01-24 15:49:30.007 575-575/? v/inputmethodservice: onevaluateinputviewshown: config.hardkeyboardhidden = 1 01-24 15:49:30.011 575-597/? e/binarydictionarygetter: not find dictionary pack 01-24 15:49:30.011 575-597/? e/activitythread: failed find provider info com.android.inputmethod.latin.dictionarypack 01-24 15:49:31.167 1765-1765/? d/androidruntime: shutting down vm 01-24 15:49:31.167 1765-1765/? w/dalvikvm: threadid=1: thread exiting uncaught exception (group=0xa6206908) 01-24 15:49:31.167 1765-1765/? e/androidruntime: fatal exception: main 01-24 15:49:31.167 1765-1765/? e/androidruntime: java.lang.nullpointerexception 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ android.database.sqlite.sqliteopenhelper.getdatabaselocked(sqliteopenhelper.java:224) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:164) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ com.waiyanhein.todo.todo.databasehelper.inserttask(databasehelper.java:46) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ com.waiyanhein.todo.todo.createtaskfragment.savetask(createtaskfragment.java:100) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ com.waiyanhein.todo.todo.createtaskfragment$2.onclick(createtaskfragment.java:49) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ android.view.view.performclick(view.java:4204) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ android.view.view$performclick.run(view.java:17355) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ android.os.handler.handlecallback(handler.java:725) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ android.os.handler.dispatchmessage(handler.java:92) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ android.os.looper.loop(looper.java:137) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ android.app.activitythread.main(activitythread.java:5041) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ java.lang.reflect.method.invokenative(native method) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ java.lang.reflect.method.invoke(method.java:511) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 01-24 15:49:31.167 1765-1765/? e/androidruntime:     @ dalvik.system.nativestart.main(native method) 01-24 15:49:31.167 415-426/? w/activitymanager:   force finishing activity com.waiyanhein.todo.todo/.mainactivity 01-24 15:49:31.231 415-426/? d/dalvikvm: gc_for_alloc freed 1480k, 53% free 6135k/12844k, paused 5ms, total 6ms 01-24 15:49:31.239 415-418/? d/dalvikvm: gc_concurrent freed 46k, 47% free 6925k/12844k, paused 2ms+1ms, total 10ms 01-24 15:49:31.251 415-431/? d/dalvikvm: gc_for_alloc freed 107k, 45% free 7160k/12844k, paused 7ms, total 7ms 01-24 15:49:31.263 415-418/? d/dalvikvm: gc_concurrent freed 1k, 36% free 8260k/12844k, paused 1ms+0ms, total 10ms 01-24 15:49:31.263 415-431/? d/dalvikvm: wait_for_concurrent_gc blocked 3ms 01-24 15:49:31.267 415-431/? i/dalvikvm-heap: grow heap (frag case) 10.614mb 2536932-byte allocation 01-24 15:49:31.279 415-423/? d/dalvikvm: gc_for_alloc freed <1k, 30% free 10737k/15324k, paused 13ms, total 13ms 01-24 15:49:31.743 415-428/? w/activitymanager: activity pause timeout activityrecord{5325c494 u0 com.waiyanhein.todo.todo/.mainactivity} 01-24 15:49:31.747 415-470/? i/qtaguid: failed write_ctrl(s 1 10023) res=-1 errno=1 01-24 15:49:31.747 415-470/? w/networkmanagementsockettagger: setkernelcountset(10023, 1) failed errno -1 01-24 15:49:31.791 598-598/? w/egl_genymotion: eglsurfaceattrib not implemented 01-24 15:49:32.359 415-470/? i/qtaguid: failed write_ctrl(s 0 10062) res=-1 errno=1 01-24 15:49:32.359 415-470/? w/networkmanagementsockettagger: setkernelcountset(10062, 0) failed errno -1 01-24 15:49:42.307 415-428/? w/activitymanager: activity destroy timeout activityrecord{5325c494 u0 com.waiyanhein.todo.todo/.mainactivity} 

the error null pointer exception on line 101 of fragment. it's hard tell post line 101 represents, i'm assuming line has:

dbhelper.inserttask(task); 

you declare dbhelper member variable @ top of class, never instantiate it. instead, call:

new databasehelper(getactivity()); 

without ever attaching dbhelper. change line to:

dbhelper = new databasehelper(getactivity()); 

it fix error.


Comments