Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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);

}