Search code examples
javaandroidsqliteandroid-sqlite

Displaying email in navigation drawer after logging in


I am trying to show email from login.java page in the navigation drawer after they login to Android App. I am getting a FATAL EXCEPTION error and java.lang.NullPointerException when I run.

I have put code into DoctorHome.java:

package com.example.medicationmanagementsystem;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.example.medicationmanagementsystem.DAO.DatabaseHelper;
import com.google.android.material.navigation.NavigationView;

public class DoctorHome extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private DrawerLayout drawer;
    TextView txtDisplayEmail;
    DatabaseHelper myDb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.doctor_layout);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        drawer = findViewById(R.id.drawer_layout);

        txtDisplayEmail = findViewById(R.id.emaildisplay);
        txtDisplayEmail.setText(myDb.getEmail());

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    }
    //END

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.nav_createprescription:
                Intent createpresintent = new Intent(DoctorHome.this, MainActivity.class);
                startActivity(createpresintent);
                break;
            case R.id.viewprescriptions:
                Intent presintent = new Intent(DoctorHome.this, ViewListContents.class);
                startActivity(presintent);
                break;
            case R.id.nav_addpatients:
                Intent createpatientintent = new Intent(DoctorHome.this, NewPatient.class);
                startActivity(createpatientintent);
                break;
            case R.id.nav_viewpatients:
                Intent viewpatientintent = new Intent(DoctorHome.this, ViewPatientListContents.class);
                startActivity(viewpatientintent);
                break;
            case R.id.nav_logout:
                Intent logoutintent= new Intent(DoctorHome.this, Login.class);
                startActivity(logoutintent);
                break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}

DatabaseHelper.java

//getting email
    public String getEmail() throws SQLException {
        String email = "";
        Cursor cursor = this.getReadableDatabase().query(
                TABLE_USERS, new String[] {COL_USER_EMAIL},
                null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                email = cursor.getString(0);
            } while (cursor.moveToNext());
        }
        cursor.close();

        return email;
    }

I am using SQLite as the database to save user details. I just need the email from this to display in the textview emaildisplay in the navigation header. Any help would be appreciated. Thank you!!


Solution

  • First you have to initialize myDb before using it like this:

    myDb = new DatabaseHelper(this);
    

    Also txtDisplayEmail is not inside your activity's layout, so this:

    txtDisplayEmail = findViewById(R.id.emaildisplay);
    

    returns null.
    What you should do is find txtDisplayEmail inside the NavigationView's header.
    Change to this:

    myDb = new DatabaseHelper(this);
    drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    View header = navigationView.getHeaderView(0);
    txtDisplayEmail = (TextView) header.findViewById(R.id.emaildisplay);
    txtDisplayEmail.setText(myDb.getEmail());
    navigationView.setNavigationItemSelectedListener(this);
    


    But the method getEmail() of DatabaseHelper does not return the user's email (if this is what it is supposed to do).
    It just loops over all the emails of the table and returns the last one.
    So change it to return the email of the logged in user.