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

Null Pointer Exception

This is a class where I put some of SectionPagerAdapter into, like the video. I'm getting NullPointerException at getPageTitle()method's case 0: return line; i.e.return mContext.getString(R.string.title_section1).toUpperCase(l);

string value is stored to proper place, and what's more, when I put hardcode "Inbox" as a return of case 0: , it also has null pointer exception. so it doesn't seem to be nullpointerexception itself to cause the problem.

Does anyone have similar experience?

import java.util.Locale;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public Context mContext;

        public SectionsPagerAdapter(Context context, FragmentManager fm) {
            super(fm);
            mContext = context;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0: 
                    return new InboxFragment();
                case 1: 
                    return new SendFragment();
                case 2: 
                    return new FriendsFragment();
            }
            return null;

        }

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

        @Override
        public CharSequence getPageTitle(int position) { 
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return mContext.getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return mContext.getString(R.string.title_section2).toUpperCase(l);
                case 2:
                    return mContext.getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Can you verify that mContext is being set correctly? Try inserting log statements or a break point and debug before that line.

That is the answer. Thank you so much Ben!! It was just silly. As you expected, Log displayed mContext = null and it turned that I was passing "mContext, which is of course null, from MainActivity", and then set it as mContext in Adapter class.