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 Android Fragments Managing Fragments Adding the ViewPager

chandrashekhar singh
chandrashekhar singh
507 Points

I am getting this problem

Process: com.csing1s.myapplication, PID: 12912 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference at com.csing1s.myapplication.ViewPagerFragment.onCreateView(ViewPagerFragment.java:36) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:155)

1 Answer

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Chandrashekhar! Let's see if we can't reason through what this error might mean. It looks like you're trying to call, 'setAdapter' on a null object reference. So I'd guess it's the 'viewPager' that's null. Then, since we set the 'viewPager' by using the 'findViewById' funcion, and since the 'findViewById' function returns null if it doesn't find that id. I'm going to guess that the id you're passing into 'findViewById' doesn't exist.