0

I often come across null exception errors and I always end up solving them, except this time I'm stuck with this error for days now.

Here it is:

In my mainActivity I need to print some data from the database through this method

  public void printDB(){

    String dbString= WinToDB.dbToString();
    wordDB.setText(dbString);
    word.setText("");

    meaningDB.setText(dbString);
    meaning.setText("");
}

dbString should carry what this this method from my database handler is returning

 public String dbToString(){
    dbString="";
    SQLiteDatabase db = getWritableDatabase();
    String query= "SELECT * FROM  " + TABLE_NAME +  " WHERE 1 ";  // * star means all 'columns' 1 means all 'row'

    Cursor c = db.rawQuery(query, null);
    //move to first location
    c.moveToFirst();

    while (!c.isAfterLast()){
        if(c.getString(c.getColumnIndex("wishe")) != null){
            dbString += c.getString(c.getColumnIndex("wishe"));
            dbString += "\n";

        }
    }
    db.close();
   // db.releaseReference();

    return dbString;
}

But it throws attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

here's the logcat:

.RuntimeException: Unable to start activity ComponentInfo{com.mirdox.myapplication/com.mirdox.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2362) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424) at android.app.ActivityThread.access$900(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:139) at android.app.ActivityThread.main(ActivityThread.java:5298) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.mirdox.myapplication.MainActivity.printDB(MainActivity.java:79) at com.mirdox.myapplication.MainActivity.onCreate(MainActivity.java:29) at android.app.Activity.performCreate(Activity.java:6072) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)  at android.app.ActivityThread.access$900(ActivityThread.java:155)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:139)  at android.app.ActivityThread.main(ActivityThread.java:5298)  at java.lang.reflect.Method.invoke(Native Method) 

What's wrong and what should I do? Thanks in advance!

EDIT Here's the onCreate of my mainActivity `public class MainActivity extends AppCompatActivity {

EditText word, meaning;
TextView wordDB, meaningDB;
DBhandler WinToDB;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    WinToDB= new DBhandler(this, null, null, 1);
    printDB();

    //_________input________________________
    word= (EditText)findViewById(R.id.word);
    meaning= (EditText)findViewById(R.id.meaning);

    //_________output_______________________
    wordDB= (TextView)findViewById(R.id.textView);
    meaningDB= (TextView)findViewById(R.id.textView2);

****EDIT 2 Here is my content_main.xml: I'had a problem adding the code to here; here's the link to code on evernotecontent_main.xml

ps: I have the printDb(); written in onCreate in the mainAcitvity.

Sruda
  • 63
  • 7

1 Answers1

0

wordDB, word, meaningDB or meaning is null, whichever is on line 79.

Vampire
  • 35,631
  • 4
  • 76
  • 102