java - nullpointer exception in parsing string to date -


i getting null pointer exception when parsing string date. using simpledateformat parsing. pls see code below , me.

error in below line

date datefrom = formatter.parse(from2);

code 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 * "+ table_labels + " "+ key_name + " ='"+text2+"' , "+from_date+"='"+from2+"'";             string selectquery1 = "select * "+ table_labels + " "+ key_name + " ='"+text2+"'";             sqlitedatabase db = this.getreadabledatabase();               cursor cursor = db.rawquery(selectquery, null);//selectquery,selectedarguments               cursor cursor1 = db.rawquery(selectquery1, null);             // looping through rows , adding list               if (cursor.movetofirst()) {                   {                        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()) {                   {                           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 errors

`java.lang.nullpointerexception @ java.text.simpledateformat.parse(simpledateformat.java:1009) @ java.text.dateformat.parse(dateformat.java:553) @ avi.maindatabasehandler.getiimeslotaii(maindatabase 45 handler.java:580) @ avi.book_setup_usr.loadiimespinnerdataaii(book_setu 45 p_usr.java:460) @ avi.book_setup_usr.access$1(book_setup_usr.java:448 45 @ avi.book_setup_usr$8.onclick(book_setup_usr.javaz77 45 7) @ com.android.internal.app.alertcontrol1er$buttonhand1er.handleme 45 ssage(alertcontroller.java:166) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.ref1ect.method.invokenative(native method) @ java.lang.ref1ect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygo 45 teinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method)` 

first make sure from2 not null , data getting db not null.

following added success , failure cases,

string           from2            = "01-12-2015"; simpledateformat simpledatefromat = new simpledateformat("dd-mm-yyyy"); date datefrom                     = simpledatefromat.parse(from2);   system.out.println("datefrom : "+datefrom); 

here correct date : tue dec 01 00:00:00 ist 2015

from2 = "2015-12-01"; datefrom = simpledatefromat.parse(from2); system.out.println("datefrom : "+datefrom); 

here because of date not in correct format parse : tue jun 07 00:00:00 ist 7

from2 = ""; datefrom = simpledatefromat.parse(from2); system.out.println("datefrom : "+datefrom); 

here java.text.parseexception: unparseable date: ""

from2 = null; datefrom = simpledatefromat.parse(from2); system.out.println("datefrom : "+datefrom); 

here java.lang.nullpointerexception


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -