title: App crash on setting icon to tab layout
body: <p>I'm creating a tabLayout with Navigation view in android. I want to set icons to the tabs of the tab layout. The app throws a null pointer exception and crashes when i use the setIcon function. Please help me.</p>
  
  <pre><code>public class navHomeActivity extends AppCompatActivity
      implements NavigationView.OnNavigationItemSelectedListener {
  
  private TabLayout tabLayout;
  private ViewPager viewPager;
  int icons[] = {
          R.drawable.icon1,
          R.drawable.icon2,
          R.drawable.icon3};
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_nav_home);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      viewPager = (ViewPager) findViewById(R.id.homeViewPager);
      setupViewPager(viewPager);
      setupTabIcons();
  
      tabLayout = (TabLayout) findViewById(R.id.tabHomeLayout);
      tabLayout.setupWithViewPager(viewPager);
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
              this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
      drawer.setDrawerListener(toggle);
      toggle.syncState();
  
      NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
      navigationView.setNavigationItemSelectedListener(this);
  }
  
  
  private void setupTabIcons() {
      tabLayout.getTabAt(0).setIcon(icons[0]);
      tabLayout.getTabAt(1).setIcon(icons[1]);
      tabLayout.getTabAt(2).setIcon(icons[2]);
  }
  
  private void setupViewPager(ViewPager viewPager) {
      ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
      adapter.addFrag(new OneFragment(), "tab1");
      adapter.addFrag(new OneFragment(), "tab2");
      adapter.addFrag(new OneFragment(), "tab3");
      viewPager.setAdapter(adapter);
  }
  
  class ViewPagerAdapter extends FragmentPagerAdapter {
      private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
      private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
      public ViewPagerAdapter(FragmentManager manager) {
          super(manager);
      }
  
      @Override
      public Fragment getItem(int position) {
          return mFragmentList.get(position);
      }
  
      @Override
      public int getCount() {
          return mFragmentList.size();
      }
  
      public void addFrag(Fragment fragment, String title) {
          mFragmentList.add(fragment);
          mFragmentTitleList.add(title);
  
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return mFragmentTitleList.get(position);
      }
  }
  
  
  @Override
  public void onBackPressed() {
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      if (drawer.isDrawerOpen(GravityCompat.START)) {
          drawer.closeDrawer(GravityCompat.START);
      } else {
          super.onBackPressed();
      }
  }
  
  @SuppressWarnings("StatementWithEmptyBody")
  @Override
  public boolean onNavigationItemSelected(MenuItem item) {
      // Handle navigation view item clicks here.
      Fragment fragment = null;
  
      int id = item.getItemId();
  
      if (id == R.id.nav_home)
          {
              setTitle("Home");
          }
      else if (id == R.id.frag2)
          {
  
          }
      else if (id == R.id.frag3)
          {
  
          }
      else if (id == R.id.frag4)
          {
  
          }
      else if (id == R.id.frag5)
          {
  
          }
      else if (id == R.id.frag6)
          {
  
          }
      else if (id == R.id.frag7)
          {
  
          }
      else if (id == R.id.frag8)
          {
  
          }
  
      try {
          FragmentManager fragmentManager = getSupportFragmentManager();
          fragmentManager.beginTransaction().replace(R.id.homeContentLayout, fragment).commit();
      }catch (NullPointerException e){
          Log.d("Exception", e.toString());}
  
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      drawer.closeDrawer(GravityCompat.START);
      return true;
  }  
  }
  
  LogCat:
  </code></pre>
  
  <blockquote>
    <p>FATAL EXCEPTION: main
                                                                                 java.lang.RuntimeException: Unable to start activity
    ComponentInfo{com.techsponential.cricbash/com.techsponential.cricbash.navHomeActivity}:
    java.lang.NullPointerException: Attempt to invoke virtual method
    'android.support.design.widget.TabLayout$Tab
    android.support.design.widget.TabLayout.getTabAt(int)' on a null
    object reference
                                                                                     at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
                                                                                     at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
                                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                     at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual
    method 'android.support.design.widget.TabLayout$Tab
    android.support.design.widget.TabLayout.getTabAt(int)' on a null
    object reference
                                                                                     at
    com.techsponential.cricbash.navHomeActivity.setupTabIcons(navHomeActivity.java:57)
                                                                                     at
    com.techsponential.cricbash.navHomeActivity.onCreate(navHomeActivity.java:40)
                                                                                     at android.app.Activity.performCreate(Activity.java:6245)
                                                                                     at
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
                                                                                     at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                                     at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)</p>
    
    <p>at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)</p>
    
    <p>at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)</p>
  </blockquote>