-2

Before posting I checked many questions but they didn't helped me. Most of the answers says that the ViewPager is in different layout, so that is why it generating null pointer exception.

Below is my stacktrace

E/AndroidRuntime: FATAL EXCEPTION: main Process: pdfshare.hemanthreddy.com.pdfshare, PID: 29092
java.lang.RuntimeException: Unable to start activity ComponentInfo{pdfshare.hemanthreddy.com.pdfshare/pdfshare.hemanthreddy.com.pdfshare.activities.HomeScreen}: 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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: 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 pdfshare.hemanthreddy.com.pdfshare.activities.HomeScreen.onCreate(HomeScreen.java:39)
at android.app.Activity.performCreate(Activity.java:6259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

This is my xml file where I have declared Viewpager activity_home_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="pdfshare.hemanthreddy.com.pdfshare.activities.HomeScreen">
<android.support.v4.view.ViewPager
    android:id="@+id/viewpagerhome"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
 </RelativeLayout>

Activity class HomeScreen.java

package pdfshare.hemanthreddy.com.pdfshare.activities;

import android.graphics.Color;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; 
import android.widget.Toast;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.BottomBarBadge;
import com.roughike.bottombar.BottomBarTab;
 import com.roughike.bottombar.OnMenuTabSelectedListener;
import com.roughike.bottombar.OnTabSelectedListener;

import pdfshare.hemanthreddy.com.pdfshare.R;
import pdfshare.hemanthreddy.com.pdfshare.fragments.GroupsFragment;
import pdfshare.hemanthreddy.com.pdfshare.fragments.HomeFragment;
 import pdfshare.hemanthreddy.com.pdfshare.fragments.NotificationsFragment;
import pdfshare.hemanthreddy.com.pdfshare.fragments.ProfileFragment;

 public class HomeScreen extends AppCompatActivity {

ViewPager pager;
BottomBar bottomBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
    bottomBar = BottomBar.attach(this,savedInstanceState);
    pager = (ViewPager) findViewById(R.id.viewpagerhome);
    MyPagerAdapter obj = new MyPagerAdapter(getSupportFragmentManager());
    //the following two if statements are used to check if objects are null 
    if(obj.getCount()==4)
        Log.e("obj","not null");
    if(pager == null)
        Log.e("pager","null");
    pager.setAdapter(obj);
     bottomBar.setItems(new BottomBarTab(R.mipmap.ic_action_home_24,"home"),
            new BottomBarTab(R.mipmap.ic_action_user_group,"groups"),
            new   BottomBarTab(R.mipmap.ic_action_notification,"notifications"),
            new BottomBarTab(R.mipmap.ic_action_profile,"profile")
           );
    bottomBar.setOnItemSelectedListener(new OnTabSelectedListener() {
        @Override
        public void onItemSelected(int position) {
               T oast.makeText(getApplicationContext(),position,Toast.LENGTH_LONG).show();
            pager.setCurrentItem(position);
        }
    });



    BottomBarBadge message = bottomBar.makeBadgeForTabAt(2,"red",10);
    message.show();

}



private class MyPagerAdapter extends FragmentStatePagerAdapter
{

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

    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return new HomeFragment();
            case 1:
                return new GroupsFragment();
            case 2:
                return new NotificationsFragment();
            case 3:
                return new ProfileFragment();
            default:
                return new HomeFragment();
        }
    }

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


}

E/obj: not null E/pager: null dont know why pager object is null. Please help me I am trying to load 4 fragments in viewPager, thank you.

hemanth5636
  • 89
  • 2
  • 9
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jens Jul 05 '16 at 05:55
  • 1
    are you sure you are loading the right layout file, that means the posted layout in indeed set in activity – JAAD Jul 05 '16 at 05:56
  • 1
    your layout `activity_home_screen` does not contain `viewpagerhome` ViewPager. Check it once..!! – Janki Gadhiya Jul 05 '16 at 05:57
  • hey @jankigadhiya already i posted both xml and my main activity please check in my post – hemanth5636 Jul 05 '16 at 06:32
  • comment your `bottomBar` code including `attach`,`setItems`, `setOnItemSelectedListener` & `BottomBarBadge` everything expect view pager and adpater. I think the bottombar is replacing your layout..!!@hemanth5636 – Janki Gadhiya Jul 05 '16 at 06:37
  • Your code seems to be fine. Did you try a clean build already? – 0xDEADC0DE Jul 05 '16 at 06:47

1 Answers1

-2

it means your variable obj of type MyPagerAdapter is null, there is an error with it's return value.

MyPagerAdapter obj = new MyPagerAdapter(getSupportFragmentManager());

That line is the problem, the problem could be in your custom adapter, or your get support fragment manager. if they were working the obj would not be null.

i bet if you comment out that line and the setadapter line it won't crash anymore, unless you have bigger problems

Jon halls
  • 44
  • 6
  • im sure that the object is not null. right now i checked by the code if(obj.getCount()==4) Log.e("obj","not null"); – hemanth5636 Jul 05 '16 at 06:44
  • 1
    The stacktrace clearly shows that the `ViewPager` variable is null: `Caused by: 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`. – 0xDEADC0DE Jul 05 '16 at 06:46
  • You know the title of your question says null pointer exception right. – Jon halls Jul 05 '16 at 06:46
  • add the code i linked and try it before you say i'm wrong please. – Jon halls Jul 05 '16 at 06:47
  • it returned 4, so i think object is created @Jonhalls, i also checked as u said, comment the setadapter(), app worked fine. but now the obj is created and dont know why its crashing on setAdapter() – hemanth5636 Jul 05 '16 at 06:47
  • if your string comes from an object then you need to use toString() to convert it to a String, although i think you would get a mismatch if that was the case, not 100% so – Jon halls Jul 05 '16 at 06:48
  • it never hurts to check again using the code i linked, just to rule it out that there wasn't a mistake with converting variables or passing strings. your check should be immediately before the method call to be sure. – Jon halls Jul 05 '16 at 06:50
  • you should be less critical of the people who are trying to help you, people are down voting your question and you didn't even acknowledge that my answer was right. – Jon halls Jul 05 '16 at 06:51
  • @Jonhalls, you should do better analysis. It is obvious from the stacktrace and the code that the line you mention is not the problem. There is no way that the pager adapter is null at that moment which is also confirmed by OP (as it returns 4) – 0xDEADC0DE Jul 05 '16 at 06:57
  • well by process of elimination if it's not the string is must be a problem here MyPagerAdapter(getSupportFragmentManager are you sure your MyPagerAdapter is working properly. – Jon halls Jul 05 '16 at 07:00
  • i said that in my original answer that you down voted i'm done here bye good luck. – Jon halls Jul 05 '16 at 07:02