title: tablayout.newtab null pointer
body: <p>hey I am trying to use tabs on android but i am getting error when setting the tablayout </p>
  
  <p>Here is my code </p>
  
  <pre><code>TabLayout tabLayout = findViewById(R.id.tablayout);
      tabLayout.addTab(tabLayout.newTab().setText("Fruits"));
      tabLayout.addTab(tabLayout.newTab().setText("Vegetables"));
      tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  
  
      final ViewPager viewPager = findViewById(R.id.pager);
  
  
      final FruitsAdapter adapter = new FruitsAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
      viewPager.setAdapter(adapter);
      viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
      tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
          @Override
          public void onTabSelected(TabLayout.Tab tab) {
              viewPager.setCurrentItem(tab.getPosition());
          }
  
          @Override
          public void onTabUnselected(TabLayout.Tab tab) {
  
          }
  
          @Override
          public void onTabReselected(TabLayout.Tab tab) {
  
          }
      });
  </code></pre>
  
  <p>this is the error</p>
  
  <pre><code> java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mohad.grocery/com.mohad.grocery.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout.newTab()' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
      at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
      at android.os.Handler.dispatchMessage(Handler.java:106)
      at android.os.Looper.loop(Looper.java:193)
      at android.app.ActivityThread.main(ActivityThread.java:6669)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout.newTab()' on a null object reference
      at com.mohad.grocery.Main2Activity.onCreate(Main2Activity.java:30)
  </code></pre>
  
  <p>I couldn't find anyone who has the same problem as me so I asked his question</p>
  
  <p>The problem is on newtab but I dont know why</p>
  
  <p>Thank you for help</p>