When I call this method sometimes it's shows exception
public static long getTimestampFromDate(String date, String format) {
DateFormat formatter = new SimpleDateFormat(format, Locale.ENGLISH);
//formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date d = null;
try {
d = formatter.parse(date);
} catch (Exception e) {
e.printStackTrace();
Crashlytics.logException(e);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 1);
return calendar.getTimeInMillis();
}
My error :
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference
at java.util.Calendar.setTime(Calendar.java:1197)
at com.utils.Utils.getTimestampFromDate(SourceFile:105)
at com.fragment.MyProfileFragment.onViewClicked(SourceFile:768)
at com.fragment.MyProfileFragment_ViewBinding$4.doClick(SourceFile:79)
at butterknife.internal.DebouncingOnClickListener.onClick(SourceFile:22)
at android.view.View.performClick(View.java:5205)
at android.view.View$PerformClick.run(View.java:21162)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5453)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)
Date format :
public static String SLASH_MM_DD_YYYY = "dd-MMM-yyyy";
Any solutions for this?