title: Getting nullpointerexception even after giving right reference. Please see details
body: <p>I'm trying to set the name and email in <code>NavigationView</code> after fetching it from the database.</p>
  
  <p>Here's how I have given reference of <code>NavigationView</code> in <code>onCreate()</code> method:</p>
  
  <pre><code>navigationView = (NavigationView) findViewById(R.id.nav_view);
  navigationView.setNavigationItemSelectedListener(this);
  </code></pre>
  
  <p>But, I'm still getting this error: <code>Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.NavigationView.getHeaderView(int)' on a null object reference                                                                                   at com.abc.xyz.MainActivity.fetchDataFromProviderProvided(MainActivity.java:689)</code></p>
  
  <p>Here's <code>fetchDataFromProviderProvided()</code> method:</p>
  
  <pre><code>public void fetchDataFromProviderProvided() {
          if (authData != null) {
              swipeRefreshLayout.setRefreshing(false);
              if (authData.getProvider().equals("google")) {
                  fetchDataFromGoogle();
              } else if (authData.getProvider().equals("facebook")) {
                  onFacebookAccessTokenChange(AccessToken.getCurrentAccessToken());
              } else {
  
                  // DO SOMETHING TO FETCH USER'S NAME
  
                  String Uid = authData.getUid();
  
                  Log.d("Uid:", Uid);
  
                  Firebase userNameRef = mFirebaseRef.child(Uid + "/userName");
  
                  Log.d("userName", String.valueOf(userNameRef));
  
                  emailImage = (String) authData.getProviderData().get("profileImageURL");
  
                  // error on the line below
                  View imageView = navigationView.getHeaderView(0);
                  //
                  userImage = (ImageView) imageView.findViewById(R.id.userImage);
  
                  new emailProfilePicAsync().execute(authData.getUid());
  
                  View nameView = navigationView.getHeaderView(0);
                  userName = (TextView) nameView.findViewById(R.id.userName);
  
                  userNameRef.addValueEventListener(new ValueEventListener() {
                      @Override
                      public void onDataChange(DataSnapshot dataSnapshot) {
                          userName.setText(String.valueOf(dataSnapshot.getValue()));
                      }
  
                      @Override
                      public void onCancelled(FirebaseError firebaseError) {
  
                      }
                  });
  
                  emailName = userNameText;
                  emailEmail = (String) authData.getProviderData().get("email");
  
                  View emailView = navigationView.getHeaderView(0);
                  userEmail = (TextView) emailView.findViewById(R.id.userEmail);
                  userEmail.setText(emailEmail);
  
  //                System.out.println(post.getUserName());
  //                System.out.println(post.getUserEmail());
              }
  
              dataLoadingCompleteProgressDialog = ProgressDialog.show(MainActivity.this, "",
                      "Fetching user's info...", true);
  
              if (dataLoadingCompleteProgressDialog.isShowing()) {
                  Handler handler = new Handler();
                  handler.postDelayed(new Runnable() {
                      @Override
                      public void run() {
                          checkIfDataIsFetched();
                          dataLoadingCompleteProgressDialog.dismiss();
                      }
                  }, 5500);
              }
  
          } else {
  
          }
      }
  </code></pre>
  
  <p>Here's <code>activity_main.xml</code> file's code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v4.widget.DrawerLayout
      android:id="@+id/drawer_layout"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:openDrawer="start"&gt;
  
      &lt;include
          layout="@layout/app_bar_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/&gt;
  
      &lt;android.support.design.widget.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:fitsSystemWindows="true"
          app:headerLayout="@layout/nav_header_main"
          app:menu="@menu/activity_main_drawer"/&gt;
  
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>Here's complete stacktrace:</p>
  
  <pre><code>FATAL EXCEPTION: main
                                                                                 Process: com.abc.xyz, PID: 19072
                                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.xyz/com.abc.xyz.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.NavigationView.getHeaderView(int)' on a null object reference
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.NavigationView.getHeaderView(int)' on a null object reference
                                                                                     at com.abc.xyz.MainActivity.fetchDataFromProviderProvided(MainActivity.java:694)
                                                                                     at com.abc.xyz.MainActivity.onCreate(MainActivity.java:217)
                                                                                     at android.app.Activity.performCreate(Activity.java:6237)
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  </code></pre>
  
  <p>Please let me know what's wrong here.</p>
  
  <p>Sorry, for bad formatting of the question. I'm still a beginner here.</p>