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 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.