java - App crashing during onTextChanged() -


this question has answer here:

i making simple notes app sqlite. has 2 activities , helper class sqlite. when click on example note(akhilesh chobey) in mainactivity.java, , try edit in main2activity.java application crashes.

main activity:

    public class mainactivity extends appcompatactivity {        listview noteslistview;     static arraylist<string> notesarraylist;     arrayadapter<string> adapter;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          noteslistview = (listview) findviewbyid(r.id.noteslistview);         notesarraylist = new arraylist<string>();         notesarraylist.add("akhilesh chobey");         adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, notesarraylist);         noteslistview.setadapter(adapter);          noteslistview.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> adapterview, view view, int i, long l) {                 intent intent = new intent(getapplicationcontext(), main2activity.class);                 intent.putextra("noteposition", i);                 startactivity(intent);             }         });        } } 

main2activity:

public class main2activity extends appcompatactivity implements textwatcher {  databaseoperations mydb; edittext editnote;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main2);     editnote = (edittext) findviewbyid(r.id.noteedittext);      mydb = new databaseoperations(main2activity.this);      intent intent = getintent();     int position = intent.getintextra("noteposition", -1);     if(position != -1){          editnote.settext(mainactivity.notesarraylist.get(position));      }     editnote.addtextchangedlistener(this); } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) {     if (mydb == null){      boolean isinserted = mydb.insertdata(editnote.gettext().tostring());         if(isinserted){             toast.maketext(getapplicationcontext(), "inserted", toast.length_short).show();         }else {             toast.maketext(getapplicationcontext(), "error", toast.length_short).show();     } } } 

helper class:

public class databaseoperations extends sqliteopenhelper {  public static final string databasename = "notes.db"; public static final string tablename = "notes"; public static final string col1 = "text";  public databaseoperations(context context) {     super(context, databasename, null, 1); }  @override public void oncreate(sqlitedatabase db) {     db.execsql("create table " + tablename + " (text text primary key autoincrement) "); }  @override public void onupgrade(sqlitedatabase db, int i, int i1) {      db.execsql("drop table if exists " + tablename);     oncreate(db);  }  public boolean insertdata(string note){      sqlitedatabase db = this.getwritabledatabase();     contentvalues contentvalues = new contentvalues();     contentvalues.put(col1, note);      long result = db.insert(tablename, null, contentvalues);     if(result == -1){         return false;     }else {         return true;     }  }  } 

log:

06-29 17:23:34.996 2231-2231/com.akhileshchobey.mynotes e/androidruntime: fatal exception: main                                                                       process: com.akhileshchobey.mynotes, pid: 2231                                                                       java.lang.nullpointerexception: attempt invoke virtual method 'boolean com.akhileshchobey.mynotes.databaseoperations.insertdata(java.lang.string)' on null object reference                                                                           @ com.akhileshchobey.mynotes.main2activity.ontextchanged(main2activity.java:39)                                                                           @ android.widget.textview.sendontextchanged(textview.java:7746)                                                                           @ android.widget.textview.handletextchanged(textview.java:7806)                                                                           @ android.widget.textview$changewatcher.ontextchanged(textview.java:9724)                                                                           @ android.text.spannablestringbuilder.sendtextchanged(spannablestringbuilder.java:964)                                                                           @ android.text.spannablestringbuilder.replace(spannablestringbuilder.java:515)                                                                           @ android.text.spannablestringbuilder.replace(spannablestringbuilder.java:454)                                                                           @ android.text.spannablestringbuilder.replace(spannablestringbuilder.java:33)                                                                           @ android.view.inputmethod.baseinputconnection.replacetext(baseinputconnection.java:685)                                                                           @ android.view.inputmethod.baseinputconnection.setcomposingtext(baseinputconnection.java:445)                                                                           @ com.android.internal.view.iinputconnectionwrapper.executemessage(iinputconnectionwrapper.java:340)                                                                           @ com.android.internal.view.iinputconnectionwrapper$myhandler.handlemessage(iinputconnectionwrapper.java:78)                                                                           @ android.os.handler.dispatchmessage(handler.java:102)                                                                           @ android.os.looper.loop(looper.java:135)                                                                           @ android.app.activitythread.main(activitythread.java:5264)                                                                           @ java.lang.reflect.method.invoke(native method)                                                                           @ java.lang.reflect.method.invoke(method.java:372)                                                                           @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:900)                                                                           @ com.android.internal.os.zygoteinit.main(zygoteinit.java:695) 

don't initialize mydb = null;,

initialize mydb in oncreate() of main2activity

mydb = new databaseoperations(main2activity.this); 

edit

you have initialized mydb, , not null. have change below in ontextchanged() method.

if (mydb == null) 

to

if (mydb != null) 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -