I'm trying to set the text of the navigation drawer activity from the fetched data on my database, but when i try to do so, it throws the following error
Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Am I missing something or did I do something wrong in my code?
public void fetchDb(){
String email = getIntent().getExtras().getString("EMAIL");
TextView txtName = findViewById(R.id.nameText);
TextView txtEmail = findViewById(R.id.emailText);
String name, ema;
DatabaseHelper dbh = new DatabaseHelper(this);
newDb = dbh.getReadableDatabase();
Cursor c = newDb.rawQuery("SELECT * FROM user WHERE user_email = \'" + email+"\'", null);
if (c.moveToFirst()){
do {
name = c.getString(1);
ema = c.getString(2);
txtName.setText(name);
txtEmail.setText(ema);
} while(c.moveToNext());
}
c.close();
newDb.close();
}
Null pointer try to say that the textview is null. That can happen beacause the findviewbyid is not matching with the right one on your xml. Be sure your id matches on your xml and java file
Possibly there are two situation..
If u are using older version of Android studio then u should use this TextView txtName = (TextView)findViewById(R.id.nameText);
Another possibility is that may be the layout is different where the textview is declared.
This actually fixed my problem:
How to change text of a TextView in navigation drawer header?
I was trying to set the navigation headers text from my Main Class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With