SimpleDateFormat 解析时出现空指针异常
Null Pointer Exception on SimpleDateFormat parse
我正在从日历中获取日期并使用
转换其格式
SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
date = sdf1.parse(datevalue);
} catch (ParseException e) {
e.printStackTrace();
}
if(date != null) {
Date d = new Date(date.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
return sdf.format(d);
}
return "";
但我在某些设备上遇到崩溃,而对于某些设备,它工作正常。
我不知道为什么它只发生在某些设备上。
我在 Fabric 报告中得到这个:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1044)
at java.text.DateFormat.parse(DateFormat.java:577)
at com.app.appUtilities.Utilities.convertDateFormat(Utilities.java:34)
请帮帮我。
String dateStr = "04/05/2010";
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
try {
dateObj = curFormater.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
Toast.makeText(getApplicationContext(), newDateStr, 1).show();
我正在从日历中获取日期并使用
转换其格式 SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
date = sdf1.parse(datevalue);
} catch (ParseException e) {
e.printStackTrace();
}
if(date != null) {
Date d = new Date(date.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
return sdf.format(d);
}
return "";
但我在某些设备上遇到崩溃,而对于某些设备,它工作正常。
我不知道为什么它只发生在某些设备上。
我在 Fabric 报告中得到这个:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1044)
at java.text.DateFormat.parse(DateFormat.java:577)
at com.app.appUtilities.Utilities.convertDateFormat(Utilities.java:34)
请帮帮我。
String dateStr = "04/05/2010";
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
try {
dateObj = curFormater.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
Toast.makeText(getApplicationContext(), newDateStr, 1).show();