android - NullPointerException while trying to insert records -
i trying insert records db .the problem when click save button getting exception.so firstly have checked logcat , filtered out theese messages .
03-21 18:07:00.820: e/androidruntime(4756): fatal exception: main 03-21 18:07:00.820: e/androidruntime(4756): java.lang.nullpointerexception 03-21 18:07:00.820: e/androidruntime(4756): @ com.example.kjk.mainactivity$1.onclick(mainactivity.java:51) 03-21 18:07:00.820: e/androidruntime(4756): @ android.view.view.performclick(view.java:4240) 03-21 18:07:00.820: e/androidruntime(4756): @ android.view.view$performclick.run(view.java:17721) 03-21 18:07:00.820: e/androidruntime(4756): @ android.os.handler.handlecallback(handler.java:730) 03-21 18:07:00.820: e/androidruntime(4756): @ android.os.handler.dispatchmessage(handler.java:92) 03-21 18:07:00.820: e/androidruntime(4756): @ android.os.looper.loop(looper.java:137) 03-21 18:07:00.820: e/androidruntime(4756): @ android.app.activitythread.main(activitythread.java:5103) 03-21 18:07:00.820: e/androidruntime(4756): @ java.lang.reflect.method.invokenative(native method) 03-21 18:07:00.820: e/androidruntime(4756): @ java.lang.reflect.method.invoke(method.java:525) 03-21 18:07:00.820: e/androidruntime(4756): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 03-21 18:07:00.820: e/androidruntime(4756): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 03-21 18:07:00.820: e/androidruntime(4756): @ dalvik.system.nativestart.main(native method)
i think exception comes here couldnt fin solution fix problem
adddata(name.gettext().tostring(), surname.gettext().tostring());
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); name=(edittext)findviewbyid(r.id.edtname); surname=(edittext)findviewbyid(r.id.edtsurname); btnsave=(button)findviewbyid(r.id.btnsave); btnfetch=(button)findviewbyid(r.id.btnfetch); tvname=(textview)findviewbyid(r.id.tvnmae); tvsurname=(textview)findviewbyid(r.id.tvsurname); btnsave.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { try { adddata(name.gettext().tostring(), surname.gettext().tostring()); } catch (exception e) { log.e("error ", e.getmessage()); } finally{ v1.close(); } } }); btnfetch.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { sqlitedatabase db=v1.getreadabledatabase(); cursor cursor=db.query( "info", columns, null, null, null, null, null); while(cursor.movetonext()){ string name=cursor.getstring(cursor.getcolumnindex("name")); string surname=cursor.getstring(cursor.getcolumnindex("surname")); tvname.settext(name); tvsurname.settext(surname); } } }); } private void adddata(string name,string sur) { sqlitedatabase db=v1.getwritabledatabase(); contentvalues cv=new contentvalues(); cv.put(name, sur); db.insertorthrow("info", null, cv); }
here database class
private static final string db_name="mydb"; private static final int ver=1; public veritabani(context context, string name, cursorfactory factory, int version) { super(context, db_name, null, ver); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql("create table info(name text,surname text);"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop table if exist info"); oncreate(db); }
your stacktrace says
java.lang.nullpointerexception 03-21 18:07:00.820: e/androidruntime(4756):at com.example.kjk.mainactivity$1.onclick(mainactivity.java:51)
from comments
so v1 instance of database , line 51 indicated logcat v1.close();
this indicates v1 null , have not initialized properly.
try
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); v1 = new veritabani(this);
Comments
Post a Comment