Null Exception on ViewPager Adapter – Android

Issue

I’m trying to implement ViewPager in Android with my adapter and getting this error, I’ve googled and everything seems fine but still my app is crashing, any help would be highly appreciated.

Adapter:

public class MainPagerAdapter extends FragmentPagerAdapter {

public MainPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch (position){
        case 0:
            ChatFragment.create();
        case 1:
            EmptyFragment.create();
        case 2:
            StoryFragment.create();
    }
    return  null;
}

@Override
public int getCount() {
    return 3;
}

}

MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    View back  (View) findViewById(R.id.am_background_view);
    ViewPager pager  (ViewPager) findViewById(R.id.am_view_pager);
    MainPagerAdapter adapter  new MainPagerAdapter(getSupportFragmentManager());
    pager.setAdapter(adapter);
}

}

MainActivity.xml

<RelativeLayout
xmlns:android"http://schemas.android.com/apk/res/android"
xmlns:app"http://schemas.android.com/apk/res-auto"
xmlns:tools"http://schemas.android.com/tools"
android:layout_width"match_parent"
android:layout_height"match_parent"
tools:context".MainActivity">


<View
    android:id"@+id/am_background_view"
    android:layout_height"match_parent"
    android:layout_width"match_parent"
    android:background"@color/black"
    />




<android.support.v4.view.ViewPager
    android:id"@+id/am_view_pager"
    android:layout_width"match_parent"
    android:layout_height"wrap_content"
    android:layout_alignParentTop"true"
    android:layout_alignParentStart"true" />

ChatFragment

public class ChatFragment extends Fragment {

public  static ChatFragment create() {
    return new ChatFragment();
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view  inflater.inflate(R.layout.fragment_chat, container, false);
    return view;
}

Error:

FATAL EXCEPTION: main
                                                                    Process: pk.fastech.snappy.snappy, PID: 6148
                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                                                                        at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:380)
                                                                        at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:375)
                                                                        at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:103)
                                                                        at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
                                                                        at android.support.v4.view.ViewPager.populate(ViewPager.java:1182)
                                                                        at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
                                                                        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
                                                                        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                                                                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2644)
                                                                        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1599)
                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1891)
                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1487)
                                                                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7450)
                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:695)
                                                                        at android.view.Choreographer.doFrame(Choreographer.java:631)
                                                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:158)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Solution

Your getItem method is incorrect and always returns null.

You should change it to:

@Override
public Fragment getItem(int position) {
    switch (position){
        case 0:
            return ChatFragment.create();
        case 1:
            return EmptyFragment.create();
        case 2:
            return StoryFragment.create();
    }
    return null;
}

return in Java returns the result to the calling method – but you were not returning this result – it does not “stack”.

Answered By – Ken Wolf

Leave a Comment