title: Calling ViewPager inside a Fragment
body: <p>I am designing an Onboarding screen with the help of ViewPager. The ViewPager sits inside a fragment underlying my MainActivity class. The error arises when I pass the ViewPager adapter class to the ViewPager inside my fragment! It throws NullPointerException and after breakpoint debugging I find that the inflater inside my PagerAdapter class is null.
  I have tried some alternatives to get the inflater working but no luck! I ll post my codes below:</p>
  <p>ViewPager Fragment class:</p>
  <pre><code>public class ViewPagerFragment extends Fragment {
  
  ViewPager viewpager;
  LinearLayout dotsLayout;
  SliderAdapter sliderAdapter;
  
  @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_view_pager, container, false);
  
      viewpager = (ViewPager) view.findViewById(R.id.viewPagerFragment);
      dotsLayout = (LinearLayout) view.findViewById(R.id.dotsLayout);
  
      sliderAdapter = new SliderAdapter(view.getContext());
     
      viewpager.setAdapter(sliderAdapter); //The error is thrown here
      
  
      // Inflate the layout for this fragment
      return view;
  
  }
  
  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
  
      //Toast.makeText(view.getContext(),&quot;Here&quot;,Toast.LENGTH_LONG).show();
  
  
  }}
  </code></pre>
  <p>ViewPager Adapter Class:</p>
  <pre><code>public class SliderAdapter extends PagerAdapter {
  
  Context context1;
  LayoutInflater inflater;
  
  public int[] slide_images = {
          R.drawable.band,
          R.drawable.studio,
          R.drawable.videoshoot
  };
  public String[] slide_headings = {
          &quot;REHEARSAL PAD&quot;, &quot;RECORDING STUDIO&quot;, &quot;SESSION VIDEOSHOOT&quot;
  };
  public String[] slide_desc ={
          String.valueOf(R.string.rehearsal_desc),
          String.valueOf(R.string.studio_desc),
          String.valueOf(R.string.videoshoot_desc)
  };
  
  public  SliderAdapter(Context context){
  
      context1 = context;
  }
  
  @Override
  public int getCount() {
      return slide_headings.length;
  }
  
  @Override
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
      return view== (ConstraintLayout)object;
  }
  
  @NonNull
  @Override
  public Object instantiateItem(@NonNull ViewGroup container, int position) {
  
      //ViewPager Sliding Images and Desc
        inflater = (LayoutInflater)context1.getSystemService(context1.LAYOUT_INFLATER_SERVICE);
      //inflater = LayoutInflater.from(context1);
  
      View view = inflater.inflate(R.layout.slider_layout,container,false);
  
      ImageView slideImageView = (ImageView) view.findViewById(R.id.SliderImageView);
      TextView slideHeading = (TextView) view.findViewById(R.id.SliderTextView1);
      TextView slideDesc = (TextView) view.findViewById(R.id.SliderTextView2);
  
      slideImageView.setImageResource(slide_images[position]);
      slideHeading.setText(slide_headings[position]);
      slideDesc.setText(slide_desc[position]);
  
      container.addView(view);
  
      return view;
  }
  
  @Override
  public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
  
      container.removeView((ConstraintLayout)object);
  }}
  </code></pre>
  <p>And the Exception Logs are given below:</p>
  <blockquote>
  <p>E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.soundflixstudios, PID: 13363
  java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.viewpager.widget.ViewPager.setAdapter(androidx.viewpager.widget.PagerAdapter)' on a null object reference
  at com.example.soundflixstudios.onboarding.ViewPagerFragment.onCreateView(ViewPagerFragment.java:43)
  at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
  at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
  at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
  at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
  at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
  at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
  at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
  at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
  at android.os.Handler.handleCallback(Handler.java:873)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:224)
  at android.app.ActivityThread.main(ActivityThread.java:7099)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:536)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)</p>
  </blockquote>
  <p>Any help is appreciated, and yes I have gone through multiple post in stackoverflow but didnt find any solutionhence reaching out with a new question!
  Thanks in advance!</p>
  <p>Edit: I have added the xml for &quot;view_pager_fragment&quot; layout</p>
  <pre><code>enter code here&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  android:layout_width=&quot;match_parent&quot;
  android:layout_height=&quot;match_parent&quot;
  android:background=&quot;@color/white&quot;
  android:orientation=&quot;vertical&quot;
  tools:context=&quot;.onboarding.ViewPagerFragment&quot;&gt;
  
  &lt;androidx.viewpager2.widget.ViewPager2
      android:id=&quot;@+id/sliderViewPager&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:layout_gravity=&quot;center&quot;
      android:layout_weight=&quot;1&quot;&gt;
  
  &lt;/androidx.viewpager2.widget.ViewPager2&gt;
  
  &lt;LinearLayout
      android:id=&quot;@+id/dotsLayout&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;66dp&quot;
      android:layout_gravity=&quot;center&quot;
      android:layout_weight=&quot;1&quot;
      android:orientation=&quot;horizontal&quot;&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>