我在解析字符串到日期时遇到空指针异常。我使用simpledateformat进行解析。 请参阅下面的代码并帮助我。
以下错误
日期dateFROM = formatter.parse(from2);
代码 MainDatabaseHandler.java
`public List<String> getTimeSlotATT(String text2, String from2) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String selectQuery = "SELECT * FROM "+ TABLE_LABELS + " WHERE "+ KEY_NAME + " ='"+text2+"' AND "+FROM_DATE+"='"+from2+"'";
String selectQuery1 = "SELECT * FROM "+ TABLE_LABELS + " WHERE "+ KEY_NAME + " ='"+text2+"'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);//selectQuery,selectedArguments
Cursor cursor1 = db.rawQuery(selectQuery1, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
if(cursor.getString(6)==null){
list.add(null);
}
else if(cursor.getString(6)=="FULL DAY"){
list.add(cursor.getString(6));
}
else {
list.add(cursor.getString(6));
}
} while (cursor.moveToNext());
}
if (cursor1.moveToFirst()) {
do {
try {
Date dateFROM = formatter.parse(from2);
Date dateDB = formatter.parse(cursor1.getString(4));
Date dateDB1 = formatter.parse(cursor1.getString(5));
if(dateDB.after(dateFROM)){
}
else if(dateDB.before(dateFROM)){
if(dateDB1.equals(dateFROM)||dateDB1.after(dateFROM)){
list.add(cursor1.getString(6));
}
else if(dateDB1.before(dateFROM)){
}
}
} catch (ParseException e) {
e.printStackTrace();
}
} while (cursor1.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return list;
}`
LogCat错误
`java.lang.NullPointerException
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1009)
at java.text.DateFormat.parse(DateFormat.java:553)
at avi.MainDatabaseHandler.getIimeSlotAII(MainDatabase 45
Handler.java:580)
at avi.book_setup_usr.loadIimeSpinnerDataAII(book_setu 45
p_usr.java:460)
at avi.book_setup_usr.access$1(book_setup_usr.java:448 45
at avi.book_setup_usr$8.onClick(book_setup_usr.javaz77 45
7)
at com.android.internal.app.AlertControl1er$ButtonHand1er.handleMe 45
ssage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.ref1ect.Method.invokeNative(Native Method)
at java.lang.ref1ect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(Zygo 45
teInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)`
答案 0 :(得分:1)
正如有人评论的那样,from2
必须为null,或者它的格式可能与SimpleDateFormat
对象中指定的格式不同。看看吧。
摘自SimpleDateFormat.parse(String text, ParsePosition pos):
<强>返回强>: 从字符串解析的日期。如果出现错误,则返回null。
引发:NullPointerException - 如果text或pos为null。
答案 1 :(得分:0)
首先确保from2
不是null
,而从db获取的数据不是null
。
在添加成功和失败案例后,
String from2 = "01-12-2015";
SimpleDateFormat simpleDateFromat = new SimpleDateFormat("dd-MM-yyyy");
Date dateFROM = simpleDateFromat.parse(from2);
System.out.println("dateFROM : "+dateFROM);
在这里,您将获得正确的日期:Tue Dec 01 00:00:00 IST 2015
from2 = "2015-12-01";
dateFROM = simpleDateFromat.parse(from2);
System.out.println("dateFROM : "+dateFROM);
由于日期格式不正确,因此将其解析为:Tue Jun 07 00:00:00 IST 7
from2 = "";
dateFROM = simpleDateFromat.parse(from2);
System.out.println("dateFROM : "+dateFROM);
在这里,您将获得java.text.ParseException: Unparseable date: ""
from2 = null;
dateFROM = simpleDateFromat.parse(from2);
System.out.println("dateFROM : "+dateFROM);
在这里,您将获得java.lang.NullPointerException