android - compare current date and time and retrieve data from database -
in database storing fields such name , comment , datetime(yyyy-mm-dd hh:mm). when try retrieve data using select statement, not working , showing null pointer exception code creating , inserting database
sqlitedatabase database; string st = yeartopass+"-"+monthtopass+"-"+daytopass+" "+hourtopass+":"+minutetopass; database = getapplicationcontext().openorcreatedatabase( "universalreminder", mode_private, null); database.execsql("create table if not exists birthdaytable(nameofperson varchar(20),comment varchar(150),bdatetime varchar(20));"); database.execsql("insert birthdaytable values('"+name+"','"+comment+"','"+st+"');"); toast.maketext(getapplicationcontext(),st,toast.length_long).show();
and code try select
public class customdialog extends activity{ sqlitedatabase database; textview text; calendar cal = calendar.getinstance(); public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // database.execsql("select bdatetime birthdaytable "); // create custom dialog object final dialog dialog = new dialog(customdialog.this); // include dialog.xml file dialog.setcontentview(r.layout.dialogwindow); // set dialog title dialog.settitle("custom dialog"); getname(); // set values custom dialog components - text, image , button text = (textview) dialog.findviewbyid(r.id.textview1indialog); dialog.show(); button declinebutton = (button) dialog.findviewbyid(r.id.buttonindialog); // if decline button clicked, close custom dialog declinebutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // close dialog dialog.dismiss(); } }); } public void getname(){ final string tag = "customdialog"; int year, month, day, hour, minute; year = cal.get(calendar.year); month = cal.get(calendar.month); day = cal.get(calendar.day_of_month); hour = cal.get(calendar.hour_of_day); minute = cal.get(calendar.minute); string datetimecheck = year+"-"+month+"-"+day+" "+hour+":"+minute; try{ database = getapplicationcontext().openorcreatedatabase( "universalreminder", mode_private, null); toast.maketext(getapplicationcontext(), datetimecheck+" ", toast.length_long).show(); cursor cursor = database.rawquery("select nameofperson birthdaytable bdatetime = '"+datetimecheck+"';",null); // bdatetime = '" + datetimecheck+"'",null ); //("birthdaytable", new string[] {"nameofperson"}, "bdatetime = '" + datetimecheck+"'", null, null, null, null); if(cursor.movetonext()){ text.settext(cursor.getstring(0)); } }catch(exception e){ log.e(tag, "", e); toast.maketext(getapplicationcontext(),e+ " ", toast.length_long).show(); } } }
when print exception says java.lang.nullponinterexception
my log cat entry is
04-20 11:29:26.690: e/customdialog(1142): java.lang.nullpointerexception 04-20 11:29:26.690: e/customdialog(1142): @ com.universalremainder.customdialog.getname(customdialog.java:71) 04-20 11:29:26.690: e/customdialog(1142): @ com.universalremainder.customdialog.oncreate(customdialog.java:34) 04-20 11:29:26.690: e/customdialog(1142): @ android.app.activity.performcreate(activity.java:5243) 04-20 11:29:26.690: e/customdialog(1142): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 04-20 11:29:26.690: e/customdialog(1142): @ android.app.activitythread.performlaunchactivity(activitythread.java:2140) 04-20 11:29:26.690: e/customdialog(1142): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2226) 04-20 11:29:26.690: e/customdialog(1142): @ android.app.activitythread.access$700(activitythread.java:135) 04-20 11:29:26.690: e/customdialog(1142): @ android.app.activitythread$h.handlemessage(activitythread.java:1397) 04-20 11:29:26.690: e/customdialog(1142): @ android.os.handler.dispatchmessage(handler.java:102) 04-20 11:29:26.690: e/customdialog(1142): @ android.os.looper.loop(looper.java:137) 04-20 11:29:26.690: e/customdialog(1142): @ android.app.activitythread.main(activitythread.java:4998) 04-20 11:29:26.690: e/customdialog(1142): @ java.lang.reflect.method.invokenative(native method) 04-20 11:29:26.690: e/customdialog(1142): @ java.lang.reflect.method.invoke(method.java:515) 04-20 11:29:26.690: e/customdialog(1142): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:777) 04-20 11:29:26.690: e/customdialog(1142): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:593) 04-20 11:29:26.690: e/customdialog(1142): @ dalvik.system.nativestart.main(native method)
here s wats @ line 71
text.settext(cursor.getstring(0));
and @ line 34
getname();
here s wats @ line 71
text.settext(cursor.getstring(0));
text
null
.
there's not enough context initialize exactly, like
text = (textview)dialog.findviewbyid(r.id.some_textview_id);
does trick.
Comments
Post a Comment