Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Using Fragments for Tabs Modifying Tabs from the Template

NullPointerExceptionError

Im having problems with this ViewPager, im getting an error on this line: mViewPager.setAdapter(mSectionsPagerAdapter);

how can I fix this, i'm using Android Studio, please help

4 Answers

You have to assign to the instance var called mSectionsPagerAdapter the result of the instantiation process for the SectionsPagerAdapter class.

From the code you posted above I see that you instantiate the SectionsPagerAdapter class, but you do not assign the result to the mSectionsPagerAdapter instance var. The result is that the mSectionsPagerAdapter instance var remains null and then you pass that null reference to the setAdapter() method.

You should do something like:

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

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

    // IMPORTANT: assign the newly created instance to the mSectionsPagerAdapter
    // instance var.
    mSectionsPagerAdapter = new SectionsPagerAdapter(this,getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

This should solve your NPE problem.

thanks man!!! :D

Hello Juan,

Please make sure you actually instantiate the mViewPager instance variable before you call the setAdatper() method on it. The same applies for the mSectionsPagerAdapter instance var. Somewhere in your code (before calling the setAdapter() method you should have something like:

// I don't know the actual class names here or the constructor signature (I'm just guessing)
mViewPager = new ViewPager(...)  
mSectionsPagerAdapter = new PagerAdapter(...)

// after that you can call
mViewPager.setAdapter(mSectionsPagerAdapter)

Hope this helps.

Can you post the LogCat please ?

here is part of the code that i think its giving me the error:

public class MessageActivity extends FragmentActivity {

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

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

    new SectionsPagerAdapter(this,getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}