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 trialCarla Brooks
803 PointsNullPointerExceptionError
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
Andrei Fecioru
15,059 PointsYou 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.
Andrei Fecioru
15,059 PointsHello 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.
Lance Preston
Courses Plus Student 1,389 PointsCan you post the LogCat please ?
Carla Brooks
803 Pointshere 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);
}
Carla Brooks
803 PointsCarla Brooks
803 Pointsthanks man!!! :D