Display username and user email on Navigation drawer header
<p>I've created <code>BaseActivity</code>to display navigation drawer on all activities. The activities that need navigation <code>extends</code> from <code>BaseActivity</code>.</p>

<p>I tried to change the value of the textviews in the navigation drawer to display username and user email. But I am getting <code>NullPointerException</code>as below.</p>

<pre><code>  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.mysite.myapp.MainActivity.onCreate(BaseActivity.java:100)
  at com.mysite.myapp.ActivityMain.onCreate(ActivityMain.java:22)
</code></pre>

<p><strong>nav_header.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"&gt;

    &lt;ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@android:drawable/sym_def_app_icon" /&gt;

    &lt;TextView
        android:id="@+id/userView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
      android:textAppearance="@style/TextAppearance.AppCompat.Body1" /&gt;

    &lt;TextView
        android:id="@+id/EmailView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p><strong>BaseActivity.java</strong></p>

<pre><code>public abstract class BaseActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
    private SQLiteHandler db;
    private SessionManager session;
    private Toolbar mActionBarToolbar;
    private DrawerLayout mDrawerLayout;
    protected NavigationView mNavigationView;
    private ActionBarDrawerToggle mToggle;
    private TextView txtName;
    private TextView txtEmail;


    protected boolean useToolbar() {
        return true;
    }


    protected boolean useDrawerToggle() {
        return true;
    }


    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);

        getActionBarToolbar();

        setupNavDrawer();
    }//end setContentView


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        txtEmail = (TextView) findViewById(R.id.EmailView);
        txtName = (TextView) findViewById(R.id.userView);
        db = new SQLiteHandler(getApplicationContext());

        // session manager
        session = new SessionManager(getApplicationContext());

        if (!session.isLoggedIn()) {
            session.setLogin(false);

            db.deleteUsers();

            // Launching the login activity
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            // finish();
        }

        // Fetching user details from sqlite
        HashMap&lt;String, String&gt; user = db.getUserDetails();

        String name = user.get("name");
        String email = user.get("email");
        //Toast Message to show the usernmae
        Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();

         txtName.setText(name);
         txtEmail.setText(email);

    }}
</code></pre>

<p><strong>More Details</strong>
As a base activity, I am using it to create user session once the user login. I am fetching the username and user email from the SQLite database. I also tried testing whether the username is being fetched correctly through a toast message as indicated in the code. The username is being fetched correctly.</p>

<p>Please let me know if any details to be added. Any help would be appreciated.</p>