android - Attempt to invoke virtual method 'long java.util.Date.getTime()' -


this question has answer here:

i'm new android , little bit confused on java program. in program, have created 4 time picker used pick time in , time out. after that, edittext display total hours.

 simpledateformat format = new simpledateformat("hh:mm");             date dateb=null;             date datec=null;             date dateb1=null;             date datec1=null;             date dateb2=null;             date datec2=null;             date dateb3=null;             date datec3=null;               try {                     dateb = format.parse(b);                     datec = format.parse(c);                     long difference = datec.gettime() - dateb.gettime();                     int minutes = (int) ((difference / (1000 * 60)) % 60);                     int hours = (int) ((difference / (1000 * 60 * 60)) % 24) - 1;                     edittexth1.settext((hours + ":" + minutes));                 } catch (exception e) {                      system.err.println("ouch!");                 }                     try {                           dateb1 = format.parse(d);                          datec1 = format.parse(e1);                         long difference1 = datec1.gettime() - dateb1.gettime();                         int minutes1 = (int) ((difference1 / (1000 * 60)) % 60);                         int hours1 = (int) ((difference1 / (1000 * 60 * 60)) % 24) - 1;                         edittexth2.settext((hours1 + ":" + minutes1));                     } catch (exception e) {                          system.err.println("ouch!");                     }             try {                  dateb2 = format.parse(f);                  datec2 = format.parse(g);                 long difference2 = datec2.gettime() - dateb2.gettime();                 int minutes2 = (int) ((difference2 / (1000 * 60)) % 60);                 int hours2 = (int) ((difference2 / (1000 * 60 * 60)) % 24) - 1;                 edittexth3.settext((hours2 + ":" + minutes2));             } catch (exception e) {                  system.err.println("ouch!");             }             try {                  dateb3 = format.parse(h);                  datec3 = format.parse(i);                 long difference3 = datec3.gettime() - dateb3.gettime();                 int minutes3 = (int) ((difference3 / (1000 * 60)) % 60);                 int hours3 = (int) ((difference3 / (1000 * 60 * 60)) % 24) - 1;                 edittexth4.settext((hours3 + ":" + minutes3));             }catch (exception e) {                  system.err.println("ouch!");             }  // display total hours                  long dateb_sum = dateb.gettime() + dateb1.gettime() + dateb2.gettime()+dateb3.gettime();                 long datec_sum = datec.gettime() + datec1.gettime() + datec2.gettime()+datec3.gettime();                 long difference4 = datec_sum - dateb_sum;                 int minutes4 = (int) ((difference4/ (1000*60)) % 60);                 int hours4  = (int) ((difference4/ (1000*60*60)) % 24)-1;                 edittext8.settext((hours4+":"+minutes4));                  }               } 

when run app, crashed on time picker , project forced stop.i have initialized edittext still getting error. can me? thanks

logcat

 10-01 01:21:13.041    4552-4552/com.example.project.project e/androidruntime﹕ fatal exception: main     process: com.example.project.project, pid: 4552     java.lang.nullpointerexception: attempt invoke virtual method 'long java.util.date.gettime()' on null object reference             @ com.example.project.project.workdetailstable$timepick.ontimeset(workdetailstable.java:381)             @ android.app.timepickerdialog.onclick(timepickerdialog.java:145)             @ com.android.internal.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:163)             @ 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) 

i'm pretty sure error come coding below because doesn't crashed when remove them. need them calculate overall total hours!!

long dateb_sum = dateb.gettime() + dateb1.gettime() + dateb2.gettime()+dateb3.gettime();                     long datec_sum = datec.gettime() + datec1.gettime() + datec2.gettime()+datec3.gettime();                     long difference4 = datec_sum - dateb_sum;                     int minutes4 = (int) ((difference4/ (1000*60)) % 60);                     int hours4  = (int) ((difference4/ (1000*60*60)) % 24)-1;                     edittext8.settext((hours4+":"+minutes4)); 

besides crash, think using lot of repeated code, can clean up. use below method. pass edittext , dates in params , call as want.

setmytime(edittext edittext,string date1, string date2){    simpledateformat format = new simpledateformat("hh:mm");    try {              date d1 = format.parse(date1);              date d2 = format.parse(date2);             long difference = d1.gettime() - d2.gettime();             int minutes = (int) ((difference / (1000 * 60)) % 60);             int hours = (int) ((difference / (1000 * 60 * 60)) % 24) - 1;             edittext.settext((hours + ":" + minutes));         }catch (exception e) {              system.err.println(e.printstacktrace());         } } 

example:

setmytime(edittexth1,b,c); setmytime(edittexth2,d,e); setmytime(edittexth3,f,g); setmytime(edittexth4,h,i); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -