TabLayout getting null value / NullPointerException












0















I am trying to make a three tab layout, but my code keeps returning a NullPointerException!
I made another project just for that (following this tutorial: https://examples.javacodegeeks.com/android/core/widget/android-tablayout-example/ ), and everything worked perfectly. But when I made the same changes to my original project, "TabLayout" kept receiving "null", causing the application to crash when I request that specific activity. I tried debugging, but as I'm new to all of this, I couldn't figure anything out of it.



Here is the code from the activity that's gonna "host" the tabs and stuff:



public class Dashboard extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Here is where the code gets the Null value:
TabLayout tabLayout = ( TabLayout) findViewById(R.id.tab_layout);

//Here is where the app crashes (on "First Tab", because of the same Null value):
tabLayout.addTab(tabLayout.newTab().setText("First Tab"));
tabLayout.addTab(tabLayout.newTab().setText("Second Tab"));
tabLayout.addTab(tabLayout.newTab().setText("Third Tab"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(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) {
}
});
}
}


This is the message I get:



Process: festei.fun.festeisocialandroid, PID: 7285
java.lang.RuntimeException: Unable to start activity ComponentInfo{festei.fun.festeisocialandroid/festei.fun.festeisocialandroid.Dashboard}: 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:2830)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2909)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6592)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
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 festei.fun.festeisocialandroid.Dashboard.onCreate(Dashboard.java:19)
at android.app.Activity.performCreate(Activity.java:6984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2783)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2909) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6592) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)









share|improve this question





























    0















    I am trying to make a three tab layout, but my code keeps returning a NullPointerException!
    I made another project just for that (following this tutorial: https://examples.javacodegeeks.com/android/core/widget/android-tablayout-example/ ), and everything worked perfectly. But when I made the same changes to my original project, "TabLayout" kept receiving "null", causing the application to crash when I request that specific activity. I tried debugging, but as I'm new to all of this, I couldn't figure anything out of it.



    Here is the code from the activity that's gonna "host" the tabs and stuff:



    public class Dashboard extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Here is where the code gets the Null value:
    TabLayout tabLayout = ( TabLayout) findViewById(R.id.tab_layout);

    //Here is where the app crashes (on "First Tab", because of the same Null value):
    tabLayout.addTab(tabLayout.newTab().setText("First Tab"));
    tabLayout.addTab(tabLayout.newTab().setText("Second Tab"));
    tabLayout.addTab(tabLayout.newTab().setText("Third Tab"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
    (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) {
    }
    });
    }
    }


    This is the message I get:



    Process: festei.fun.festeisocialandroid, PID: 7285
    java.lang.RuntimeException: Unable to start activity ComponentInfo{festei.fun.festeisocialandroid/festei.fun.festeisocialandroid.Dashboard}: 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:2830)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2909)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6592)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
    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 festei.fun.festeisocialandroid.Dashboard.onCreate(Dashboard.java:19)
    at android.app.Activity.performCreate(Activity.java:6984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2783)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2909) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6592) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)









    share|improve this question



























      0












      0








      0








      I am trying to make a three tab layout, but my code keeps returning a NullPointerException!
      I made another project just for that (following this tutorial: https://examples.javacodegeeks.com/android/core/widget/android-tablayout-example/ ), and everything worked perfectly. But when I made the same changes to my original project, "TabLayout" kept receiving "null", causing the application to crash when I request that specific activity. I tried debugging, but as I'm new to all of this, I couldn't figure anything out of it.



      Here is the code from the activity that's gonna "host" the tabs and stuff:



      public class Dashboard extends AppCompatActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      //Here is where the code gets the Null value:
      TabLayout tabLayout = ( TabLayout) findViewById(R.id.tab_layout);

      //Here is where the app crashes (on "First Tab", because of the same Null value):
      tabLayout.addTab(tabLayout.newTab().setText("First Tab"));
      tabLayout.addTab(tabLayout.newTab().setText("Second Tab"));
      tabLayout.addTab(tabLayout.newTab().setText("Third Tab"));
      tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

      final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
      final PagerAdapter adapter = new PagerAdapter
      (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) {
      }
      });
      }
      }


      This is the message I get:



      Process: festei.fun.festeisocialandroid, PID: 7285
      java.lang.RuntimeException: Unable to start activity ComponentInfo{festei.fun.festeisocialandroid/festei.fun.festeisocialandroid.Dashboard}: 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:2830)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2909)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606)
      at android.os.Handler.dispatchMessage(Handler.java:105)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6592)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
      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 festei.fun.festeisocialandroid.Dashboard.onCreate(Dashboard.java:19)
      at android.app.Activity.performCreate(Activity.java:6984)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2783)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2909) 
      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606) 
      at android.os.Handler.dispatchMessage(Handler.java:105) 
      at android.os.Looper.loop(Looper.java:164) 
      at android.app.ActivityThread.main(ActivityThread.java:6592) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)









      share|improve this question
















      I am trying to make a three tab layout, but my code keeps returning a NullPointerException!
      I made another project just for that (following this tutorial: https://examples.javacodegeeks.com/android/core/widget/android-tablayout-example/ ), and everything worked perfectly. But when I made the same changes to my original project, "TabLayout" kept receiving "null", causing the application to crash when I request that specific activity. I tried debugging, but as I'm new to all of this, I couldn't figure anything out of it.



      Here is the code from the activity that's gonna "host" the tabs and stuff:



      public class Dashboard extends AppCompatActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      //Here is where the code gets the Null value:
      TabLayout tabLayout = ( TabLayout) findViewById(R.id.tab_layout);

      //Here is where the app crashes (on "First Tab", because of the same Null value):
      tabLayout.addTab(tabLayout.newTab().setText("First Tab"));
      tabLayout.addTab(tabLayout.newTab().setText("Second Tab"));
      tabLayout.addTab(tabLayout.newTab().setText("Third Tab"));
      tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

      final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
      final PagerAdapter adapter = new PagerAdapter
      (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) {
      }
      });
      }
      }


      This is the message I get:



      Process: festei.fun.festeisocialandroid, PID: 7285
      java.lang.RuntimeException: Unable to start activity ComponentInfo{festei.fun.festeisocialandroid/festei.fun.festeisocialandroid.Dashboard}: 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:2830)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2909)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606)
      at android.os.Handler.dispatchMessage(Handler.java:105)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6592)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
      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 festei.fun.festeisocialandroid.Dashboard.onCreate(Dashboard.java:19)
      at android.app.Activity.performCreate(Activity.java:6984)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2783)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2909) 
      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606) 
      at android.os.Handler.dispatchMessage(Handler.java:105) 
      at android.os.Looper.loop(Looper.java:164) 
      at android.app.ActivityThread.main(ActivityThread.java:6592) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)






      nullpointerexception android-tablayout






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 21:13







      Eduardo Ribeiro

















      asked Nov 20 '18 at 20:31









      Eduardo RibeiroEduardo Ribeiro

      12




      12
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401055%2ftablayout-getting-null-value-nullpointerexception%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401055%2ftablayout-getting-null-value-nullpointerexception%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy