Swipeable Views Null Exception Error - Android Studio [duplicate]










-1
















This question already has an answer here:



  • What is a NullPointerException, and how do I fix it?

    12 answers



When trying to run the app and swipe between views the app crashes with a null object reference error. I have double checked my getItem method to make sure it would handle this but the app still crashes. The application runs the first view fine but crashes when trying to swipe to the second and third.



Will appreciate if anyone can clarify what the error is.



I have also included the error log below the code.



public class MainActivity extends AppCompatActivity 

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();

);




@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;


@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;


return super.onOptionsItemSelected(item);



public class SectionsPagerAdapter extends FragmentPagerAdapter

public SectionsPagerAdapter(FragmentManager fm)
super(fm);


@Override
public Fragment getItem(int position)
//returning the current tabs
switch (position)
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 3:
Tab3 tab3 = new Tab3();
return tab3;
default:
return null;



@Override
public int getCount()
// Show 3 total pages.
return 3;





This is the error Log;




java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.Class java.lang.Object.getClass()' on a null object
reference
at
android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
at
android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
at
android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:104)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:267)
at
android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:655)
at
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)











share|improve this question















marked as duplicate by Fantômas android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 '18 at 19:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















    -1
















    This question already has an answer here:



    • What is a NullPointerException, and how do I fix it?

      12 answers



    When trying to run the app and swipe between views the app crashes with a null object reference error. I have double checked my getItem method to make sure it would handle this but the app still crashes. The application runs the first view fine but crashes when trying to swipe to the second and third.



    Will appreciate if anyone can clarify what the error is.



    I have also included the error log below the code.



    public class MainActivity extends AppCompatActivity 

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener()
    @Override
    public void onClick(View view)
    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
    .setAction("Action", null).show();

    );




    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;


    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings)
    return true;


    return super.onOptionsItemSelected(item);



    public class SectionsPagerAdapter extends FragmentPagerAdapter

    public SectionsPagerAdapter(FragmentManager fm)
    super(fm);


    @Override
    public Fragment getItem(int position)
    //returning the current tabs
    switch (position)
    case 0:
    Tab1 tab1 = new Tab1();
    return tab1;
    case 1:
    Tab2 tab2 = new Tab2();
    return tab2;
    case 3:
    Tab3 tab3 = new Tab3();
    return tab3;
    default:
    return null;



    @Override
    public int getCount()
    // Show 3 total pages.
    return 3;





    This is the error Log;




    java.lang.NullPointerException: Attempt to invoke virtual method
    'java.lang.Class java.lang.Object.getClass()' on a null object
    reference
    at
    android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
    at
    android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
    at
    android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:104)
    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
    at android.support.v4.view.ViewPager$3.run(ViewPager.java:267)
    at
    android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:655)
    at
    android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at
    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)











    share|improve this question















    marked as duplicate by Fantômas android
    Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Nov 11 '18 at 19:10


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      -1












      -1








      -1









      This question already has an answer here:



      • What is a NullPointerException, and how do I fix it?

        12 answers



      When trying to run the app and swipe between views the app crashes with a null object reference error. I have double checked my getItem method to make sure it would handle this but the app still crashes. The application runs the first view fine but crashes when trying to swipe to the second and third.



      Will appreciate if anyone can clarify what the error is.



      I have also included the error log below the code.



      public class MainActivity extends AppCompatActivity 

      private SectionsPagerAdapter mSectionsPagerAdapter;
      private ViewPager mViewPager;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      // Create the adapter that will return a fragment for each of the three
      // primary sections of the activity.
      mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

      // Set up the ViewPager with the sections adapter.
      mViewPager = (ViewPager) findViewById(R.id.container);
      mViewPager.setAdapter(mSectionsPagerAdapter);

      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener()
      @Override
      public void onClick(View view)
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
      .setAction("Action", null).show();

      );




      @Override
      public boolean onCreateOptionsMenu(Menu menu)
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;


      @Override
      public boolean onOptionsItemSelected(MenuItem item)
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings)
      return true;


      return super.onOptionsItemSelected(item);



      public class SectionsPagerAdapter extends FragmentPagerAdapter

      public SectionsPagerAdapter(FragmentManager fm)
      super(fm);


      @Override
      public Fragment getItem(int position)
      //returning the current tabs
      switch (position)
      case 0:
      Tab1 tab1 = new Tab1();
      return tab1;
      case 1:
      Tab2 tab2 = new Tab2();
      return tab2;
      case 3:
      Tab3 tab3 = new Tab3();
      return tab3;
      default:
      return null;



      @Override
      public int getCount()
      // Show 3 total pages.
      return 3;





      This is the error Log;




      java.lang.NullPointerException: Attempt to invoke virtual method
      'java.lang.Class java.lang.Object.getClass()' on a null object
      reference
      at
      android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
      at
      android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
      at
      android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:104)
      at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
      at android.support.v4.view.ViewPager$3.run(ViewPager.java:267)
      at
      android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
      at android.view.Choreographer.doCallbacks(Choreographer.java:723)
      at android.view.Choreographer.doFrame(Choreographer.java:655)
      at
      android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
      at android.os.Handler.handleCallback(Handler.java:790)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6494)
      at java.lang.reflect.Method.invoke(Native Method)
      at
      com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)











      share|improve this question

















      This question already has an answer here:



      • What is a NullPointerException, and how do I fix it?

        12 answers



      When trying to run the app and swipe between views the app crashes with a null object reference error. I have double checked my getItem method to make sure it would handle this but the app still crashes. The application runs the first view fine but crashes when trying to swipe to the second and third.



      Will appreciate if anyone can clarify what the error is.



      I have also included the error log below the code.



      public class MainActivity extends AppCompatActivity 

      private SectionsPagerAdapter mSectionsPagerAdapter;
      private ViewPager mViewPager;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      // Create the adapter that will return a fragment for each of the three
      // primary sections of the activity.
      mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

      // Set up the ViewPager with the sections adapter.
      mViewPager = (ViewPager) findViewById(R.id.container);
      mViewPager.setAdapter(mSectionsPagerAdapter);

      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener()
      @Override
      public void onClick(View view)
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
      .setAction("Action", null).show();

      );




      @Override
      public boolean onCreateOptionsMenu(Menu menu)
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;


      @Override
      public boolean onOptionsItemSelected(MenuItem item)
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings)
      return true;


      return super.onOptionsItemSelected(item);



      public class SectionsPagerAdapter extends FragmentPagerAdapter

      public SectionsPagerAdapter(FragmentManager fm)
      super(fm);


      @Override
      public Fragment getItem(int position)
      //returning the current tabs
      switch (position)
      case 0:
      Tab1 tab1 = new Tab1();
      return tab1;
      case 1:
      Tab2 tab2 = new Tab2();
      return tab2;
      case 3:
      Tab3 tab3 = new Tab3();
      return tab3;
      default:
      return null;



      @Override
      public int getCount()
      // Show 3 total pages.
      return 3;





      This is the error Log;




      java.lang.NullPointerException: Attempt to invoke virtual method
      'java.lang.Class java.lang.Object.getClass()' on a null object
      reference
      at
      android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
      at
      android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
      at
      android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:104)
      at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
      at android.support.v4.view.ViewPager$3.run(ViewPager.java:267)
      at
      android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
      at android.view.Choreographer.doCallbacks(Choreographer.java:723)
      at android.view.Choreographer.doFrame(Choreographer.java:655)
      at
      android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
      at android.os.Handler.handleCallback(Handler.java:790)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6494)
      at java.lang.reflect.Method.invoke(Native Method)
      at
      com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)






      This question already has an answer here:



      • What is a NullPointerException, and how do I fix it?

        12 answers







      java android android-studio view android-tablayout






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 '18 at 16:59









      Mostafa Arian Nejad

      6051620




      6051620










      asked Nov 11 '18 at 16:01









      coding95coding95

      138




      138




      marked as duplicate by Fantômas android
      Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 11 '18 at 19:10


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Fantômas android
      Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 11 '18 at 19:10


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The problem is in this code:




          @Override
          public Fragment getItem(int position)
          //returning the current tabs
          switch (position)
          case 0:
          Tab1 tab1 = new Tab1();
          return tab1;
          case 1:
          Tab2 tab2 = new Tab2();
          return tab2;
          case 3:
          Tab3 tab3 = new Tab3();
          return tab3;
          default:
          return null;





          Specifically, this line:




          case 3:



          This should be case 2 instead. As it is now, you'll return null from the default case for your third page.




          Incidentally, I recommend changing how you handle situations like this going forward. Instead of returning null from the default case, I think you should do this:



          case default:
          throw new IllegalArgumentException("unexpected position: " + position);


          If you'd had this in place instead of return null, your error would have been much more obvious. You'd have seen




          IllegalArgumentException: unexpected position: 2




          Which would have helped you realize the typo in case 3.



          In general, you should always throw an exception instead of returning some default value when your program is in a place it shouldn't be.






          share|improve this answer























          • It worked thanks for that. Would it be wise to print stacktrace under default?

            – coding95
            Nov 11 '18 at 18:03






          • 1





            If you throw an exception, you'll get a stacktrace by default

            – Ben P.
            Nov 11 '18 at 18:35

















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The problem is in this code:




          @Override
          public Fragment getItem(int position)
          //returning the current tabs
          switch (position)
          case 0:
          Tab1 tab1 = new Tab1();
          return tab1;
          case 1:
          Tab2 tab2 = new Tab2();
          return tab2;
          case 3:
          Tab3 tab3 = new Tab3();
          return tab3;
          default:
          return null;





          Specifically, this line:




          case 3:



          This should be case 2 instead. As it is now, you'll return null from the default case for your third page.




          Incidentally, I recommend changing how you handle situations like this going forward. Instead of returning null from the default case, I think you should do this:



          case default:
          throw new IllegalArgumentException("unexpected position: " + position);


          If you'd had this in place instead of return null, your error would have been much more obvious. You'd have seen




          IllegalArgumentException: unexpected position: 2




          Which would have helped you realize the typo in case 3.



          In general, you should always throw an exception instead of returning some default value when your program is in a place it shouldn't be.






          share|improve this answer























          • It worked thanks for that. Would it be wise to print stacktrace under default?

            – coding95
            Nov 11 '18 at 18:03






          • 1





            If you throw an exception, you'll get a stacktrace by default

            – Ben P.
            Nov 11 '18 at 18:35















          1














          The problem is in this code:




          @Override
          public Fragment getItem(int position)
          //returning the current tabs
          switch (position)
          case 0:
          Tab1 tab1 = new Tab1();
          return tab1;
          case 1:
          Tab2 tab2 = new Tab2();
          return tab2;
          case 3:
          Tab3 tab3 = new Tab3();
          return tab3;
          default:
          return null;





          Specifically, this line:




          case 3:



          This should be case 2 instead. As it is now, you'll return null from the default case for your third page.




          Incidentally, I recommend changing how you handle situations like this going forward. Instead of returning null from the default case, I think you should do this:



          case default:
          throw new IllegalArgumentException("unexpected position: " + position);


          If you'd had this in place instead of return null, your error would have been much more obvious. You'd have seen




          IllegalArgumentException: unexpected position: 2




          Which would have helped you realize the typo in case 3.



          In general, you should always throw an exception instead of returning some default value when your program is in a place it shouldn't be.






          share|improve this answer























          • It worked thanks for that. Would it be wise to print stacktrace under default?

            – coding95
            Nov 11 '18 at 18:03






          • 1





            If you throw an exception, you'll get a stacktrace by default

            – Ben P.
            Nov 11 '18 at 18:35













          1












          1








          1







          The problem is in this code:




          @Override
          public Fragment getItem(int position)
          //returning the current tabs
          switch (position)
          case 0:
          Tab1 tab1 = new Tab1();
          return tab1;
          case 1:
          Tab2 tab2 = new Tab2();
          return tab2;
          case 3:
          Tab3 tab3 = new Tab3();
          return tab3;
          default:
          return null;





          Specifically, this line:




          case 3:



          This should be case 2 instead. As it is now, you'll return null from the default case for your third page.




          Incidentally, I recommend changing how you handle situations like this going forward. Instead of returning null from the default case, I think you should do this:



          case default:
          throw new IllegalArgumentException("unexpected position: " + position);


          If you'd had this in place instead of return null, your error would have been much more obvious. You'd have seen




          IllegalArgumentException: unexpected position: 2




          Which would have helped you realize the typo in case 3.



          In general, you should always throw an exception instead of returning some default value when your program is in a place it shouldn't be.






          share|improve this answer













          The problem is in this code:




          @Override
          public Fragment getItem(int position)
          //returning the current tabs
          switch (position)
          case 0:
          Tab1 tab1 = new Tab1();
          return tab1;
          case 1:
          Tab2 tab2 = new Tab2();
          return tab2;
          case 3:
          Tab3 tab3 = new Tab3();
          return tab3;
          default:
          return null;





          Specifically, this line:




          case 3:



          This should be case 2 instead. As it is now, you'll return null from the default case for your third page.




          Incidentally, I recommend changing how you handle situations like this going forward. Instead of returning null from the default case, I think you should do this:



          case default:
          throw new IllegalArgumentException("unexpected position: " + position);


          If you'd had this in place instead of return null, your error would have been much more obvious. You'd have seen




          IllegalArgumentException: unexpected position: 2




          Which would have helped you realize the typo in case 3.



          In general, you should always throw an exception instead of returning some default value when your program is in a place it shouldn't be.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 '18 at 17:16









          Ben P.Ben P.

          23.8k32049




          23.8k32049












          • It worked thanks for that. Would it be wise to print stacktrace under default?

            – coding95
            Nov 11 '18 at 18:03






          • 1





            If you throw an exception, you'll get a stacktrace by default

            – Ben P.
            Nov 11 '18 at 18:35

















          • It worked thanks for that. Would it be wise to print stacktrace under default?

            – coding95
            Nov 11 '18 at 18:03






          • 1





            If you throw an exception, you'll get a stacktrace by default

            – Ben P.
            Nov 11 '18 at 18:35
















          It worked thanks for that. Would it be wise to print stacktrace under default?

          – coding95
          Nov 11 '18 at 18:03





          It worked thanks for that. Would it be wise to print stacktrace under default?

          – coding95
          Nov 11 '18 at 18:03




          1




          1





          If you throw an exception, you'll get a stacktrace by default

          – Ben P.
          Nov 11 '18 at 18:35





          If you throw an exception, you'll get a stacktrace by default

          – Ben P.
          Nov 11 '18 at 18:35



          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

          How do I collapse sections of code in Visual Studio Code for Windows?