I have to find time difference between two String values.I used the following code in my app,
public String diff(String first,String last)
{
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(first);
d2 = format.parse(last);
} catch (ParseException e) {
e.printStackTrace();
}
long diff = d2.getTime() - d1.getTime();
int diffMinutes = (int)diff / (60 * 1000);
int diffHours = (int)diff / (60 * 60 * 1000);
String res=Integer.toString(diffHours);
String res1=Integer.toString(diffMinutes);
String fin= res+res1;
return fin;
}
This is a function which accepts my two Strings and returns a String(Time Diff) which i can use in my class. But my app force closes when i am calling this function on a button click.
Here is the Logs!
Process: mypackage.com.work, PID: 3551 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String mypackage.com.work.UpdateTracker.diff(java.lang.String, java.lang.String)' on a null object reference at mypackage.com.work.UpdateTracker.onClick(UpdateTracker.java:138) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)`
Here is the code of calling that function
String in_db=txtTime.getText().toString();
String out_db=txtTime2.getText().toString();
String d=up.diff(in_db,out_db);
If I just comment this and insert just a hard coded String,its working fine!.So I think no problem with DB code
Help me out to figure this!!
Thanks in advance