2

I'm new to android and I get a little bit confused on the java program. In my program, I have created 4 time picker which used to pick up time in and time out. After that, an editText will display the 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 I run my app, it crashed on the time picker and the project forced to stop.I have initialized all my editText but still getting this error. Anyone can help 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 to invoke virtual method 'long java.util.Date.getTime()' on a null object reference
            at com.example.project.project.WorkDetailsTable$TimePick.onTimeSet(WorkDetailsTable.java:381)
            at android.app.TimePickerDialog.onClick(TimePickerDialog.java:145)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I'm pretty sure the error come from the coding below because it doesn't crashed when I remove them. But I need them to calculate the 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));
Hoo
  • 1,806
  • 7
  • 33
  • 66
  • is this should be datec1 = format.parse(e1); or datec1 = format.parse(e); – Ravi Gadipudi Sep 30 '15 at 08:53
  • I use e1 instead of e – Hoo Sep 30 '15 at 08:55
  • certainly looks like one of your Date objects is null when you try to call getTime() on it. Can't see which one it would be, though. Can you provide a stack trace? – NickJ Sep 30 '15 at 08:57
  • @NickJ Do you mean logcat? – Hoo Sep 30 '15 at 09:03
  • The error message you are getting must be occurring on the section of code near the end, without a try-catch block, but the only way that could happen is if one of your dates was not initialised, which would happen if another exception were swallowed up by your try-catch blocks. Do you have any 'ouch!' messages in the error log? – NickJ Sep 30 '15 at 09:03
  • @Hoo what is line number `WorkDetailsTable.java:381`? – SRB Bans Oct 01 '15 at 06:50
  • and try to print the error message as.. `Log.e("Exception "," "+e.getMessage());` may be you are unable to see the message in log cat but it exists.. – SRB Bans Oct 01 '15 at 06:55
  • @sourabhbans someone has helped me solved the problem...I can't post answer here since someone has marked my question as duplicated...Refer here http://stackoverflow.com/questions/32878412/get-overall-hours-from-two-time-picker – Hoo Oct 01 '15 at 06:56
  • @Hoo ok... enjoy the codding.. :) – SRB Bans Oct 01 '15 at 07:01
  • @sourabhbans kindly refer here http://stackoverflow.com/questions/32880980/how-to-save-different-data-from-different-class-to-different-database – Hoo Oct 01 '15 at 07:01

1 Answers1

3

Besides crash, I think you are using lot of repeated code, You can clean it up. Use the below method. Pass EditText and dates in params and call as much as you 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);
John
  • 8,846
  • 8
  • 50
  • 85
  • 1
    When you get an exception, it is unacceptable to just print an idiotic message like `"ouch!"`. Print the message that comes with the exception, *and* the exception class. – user207421 Sep 30 '15 at 10:03
  • @EJP It would be nice if you can help me to figure out the problem...Thanks – Hoo Sep 30 '15 at 10:11
  • Try this code and spot the issues easily.. – John Oct 01 '15 at 11:06
  • @Hoo The problem is a common or garden NPE. The solution is not to deference null pointers, as the duplicate says, along with a thousand or more other questions here. – user207421 Oct 03 '15 at 10:44
  • the problem has solved :) – Hoo Oct 03 '15 at 11:17