36

I try to use viewPager with the tablayout but when i set adapter with the view pager i'm getting error "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass() on a null object reference" i don't understand why this error is show.

view pager set Adapter:

viewPager= (ViewPager) findViewById(R.id.viewPager_id);
  viewPager.setAdapter(newViewPagerAdapterUseThisClass(getSupportFragmentManager(),14));

Adapter clss:

class ViewPagerAdapterUseThisClass extends FragmentPagerAdapter {
    int countTabItem;
     ViewPagerAdapterUseThisClass(FragmentManager fm,int contTabItem) {
        super(fm);

        this.countTabItem=contTabItem;
    }

    @Override
    public Fragment getItem(int position) {

        if(position==0){
            EnthusamTab enthusamTab=new EnthusamTab();
            return enthusamTab;
        }

        return null;
    }

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

view pager xml file:

 <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/tab_layout_id"
            android:id="@+id/viewPager_id"/>

Error log:

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:1248)
                                                                                  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:19861)
                                                                                  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                  at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
                                                                                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
                                                                                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
                                                                                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6343)
                                                                                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                                  at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                                  at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6126)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
3
  • 3
    You're telling the Adapter that it's got 14 items, but you're returning null for all of them except position 0. Commented Jan 24, 2017 at 11:10
  • sorry it's my fault thanks @MikeM. Commented Jan 24, 2017 at 11:18
  • Does the problem solved here ? Commented Feb 24, 2019 at 16:58

10 Answers 10

50

This exception occurs when you return null at getItem(int position) method of PagerAdapter.

You have to set a return value(n) at getCount() method and return a Fragment at getItem(int position) method for all (0 .. n-1) positions.

For instance, if you return 3 from getCount() method, then you must return a not-null Fragment for positions 0, 1 and 2 at getItem(int position) method. If you return a null for position 0, 1 or 2, you'll get:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference

private static class MyPagerAdapter extends FragmentPagerAdapter {
    ...

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

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new XFragment();
            case 1:
                return new YFragment();
            case 2:
                return new ZFragment();
            default:
                return null; // Problem occurs at this condition!
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

May be it's late but I want to ask how you know that is that exact reason? I just curious to know how you get the exact reason. The log is not so helpful.
@Ankur_009 FragmentPagerAdapter.instantiateItem (it is at shared log) calls getItem method. If getItem returns null NPE occurs.
I think from my understanding, first you have check onMeasue() -> populate() -> addNewItem() -> instantiateItem() will call getItem(). That was your thoughtprocess?
I was having a null initialization for Fragment. Now solved
11

This error usually comes when you have few fragments to return than the count specified. Like for example, returning count=3, and you return null on the second position. Please note that count is a zero based index.

Comments

3

Return for all counts. Try this:

Override
    public Fragment getItem(int position) {
            EnthusamTab enthusamTab=new EnthusamTab();
            return enthusamTab;

    }

Comments

3

Problem is occurring to return null in Fragment count function

 @Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new XFragment();
        case 1:
            return new YFragment();
        case 2:
            return new ZFragment();
        default:
            return null; // Problem occurs at this condition!
    }
}

Here outer bracket occurs error.

So the above-mentioned Answer is right and it's working.

public Fragment getItem(int position) {
    if (position == 0) {

        Tab1 tab1 = new Tab1();
        return tab1;
    }
    if (position == 2) {
        Tab2 tab2 = new Tab2();
        return tab2;
    } else {
        Tab3 tab3 = new Tab3();
        return tab3;
    }

}

it right answers.

Comments

2

According to what i see you are returning null in this method:

@Override
    public Fragment getItem(int position) {

        if(position==0){
            EnthusamTab enthusamTab=new EnthusamTab();
            return enthusamTab;
        }

        return null;
    }

Comments

2

Try to do this in getCount do count of all fragments you have: like 1..5

@Override
public int getCount() {
    return 5; //her 
}

Comments

2

This type of exception only occurs when you try to use method getCount() to return value more than you intialize in tabs. Change your return value.

Comments

2

Just use the code given below. It avoids null error and runs smoothly

@Override
public Fragment getItem(int position) {

      return new Tab1( );
} 

Comments

0

I got this error because I forget to set break command. Check your break operators at first.

Now I have this code:

@Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new FollowersFragment();
            case 1:
                return new PostsFragment();
            case 2:
                return new ReactsFragment();
            case 3:
                return new StoriesFragment();
            case 4:
                return new FollowersFragment();
            case 5:
                return new FollowersFragment();
            case 6:
                return new FollowersFragment();
        }
        return null;
    }

Comments

-1
public Fragment getItem(int position) {
        if (position == 0) {

            Tab1 tab1 = new Tab1();
            return tab1;
        }
        if (position == 2) {
            Tab2 tab2 = new Tab2();
            return tab2;
        } else {
            Tab3 tab3 = new Tab3();
            return tab3;
        }

    }

2 Comments

How does this relate to the original question? Please provide more context
i just had the same problem and that code worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.