title: Changing image of ImageView in Fragment using FragmentStatePagerAdapter
body: <p>I have a viewpager inside a fragment and I want to modify the content of the fragment hosted by the viewPager on runtime.</p>
  
  <p>I want to fetch data from Firebase and add the results of the query to the Adapter. Whenever a response is received on the onChildAdded, I add the data to my arraylist and call response in which data of the adapter is updated and <strong>notifyDataSetChanged</strong> is called.</p>
  
  <p>I am getting a <strong>NullPointerException</strong> when the image is changed in the <strong>FindSlideFragment</strong>, I can't figure out why is the layout not populated or If I am missing something.</p>
  
  <p>If this is not the right way to update my fragment then what would be an appropriate approach?</p>
  
  <p><strong>FindFragment.java</strong></p>
  
  <pre><code>public class FindFragment extends Fragment {
  
  private Firebase ref;
  private String name="";
  private String email="";
  private String pass="";
  private String image="";
  private String uid="";
  
  ArrayList&lt;Snap&gt; snapsList = new ArrayList&lt;&gt;();
  
  private ViewPager viewPager;
  private ScreenSlidePagerAdapter mPagerAdapter;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View root = inflater.inflate(R.layout.find_fragment, container, false);
  
      Bundle bundle = getArguments();
      uid = bundle.getString("uid");
      email = bundle.getString("email");
      pass = bundle.getString("pass");
  
  
      Firebase ref = new Firebase(Constants.FIREBASE_URL+"snaps/");
      Query queryRef = ref.orderByKey();
  
  
      queryRef.addChildEventListener(new ChildEventListener() {
  
          @Override
          public void onChildAdded(DataSnapshot snapshot, String previousChild) {
              Iterator&lt;DataSnapshot&gt; it = snapshot.getChildren().iterator();
              Snap snap = snapshot.getValue(Snap.class);
              snapsList.add(snap);
              reload();
          }
  
          @Override
          public void onChildChanged(DataSnapshot dataSnapshot, String s) {
  
          }
  
          @Override
          public void onChildRemoved(DataSnapshot dataSnapshot) {
  
          }
  
          @Override
          public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  
          }
  
          @Override
          public void onCancelled(FirebaseError firebaseError) {
  
          }
  
      });
  
  
      viewPager = (ViewPager) root.findViewById(R.id.pager);
      /** Important: Must use the child FragmentManager or you will see side effects. */
      mPagerAdapter= new ScreenSlidePagerAdapter(getChildFragmentManager(), snapsList.size(), getContext(), snapsList);
      viewPager.setAdapter(mPagerAdapter);
  
      return root;
  }
  
  private void reload() {
      mPagerAdapter.setData(snapsList);
      mPagerAdapter.setSlideCount();
      mPagerAdapter.notifyDataSetChanged();
      viewPager.invalidate();
      //viewPager.setCurrentItem(0);
  }
  
  
  /**
   * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
   * sequence.
   */
  private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
  
      private int slideCount;
      private Context context;
      ArrayList&lt;Snap&gt; snapsList = new ArrayList&lt;&gt;();
  
      public ScreenSlidePagerAdapter(FragmentManager fm, int slideCount, Context context, ArrayList&lt;Snap&gt; snaps) {
          super(fm);
          this.slideCount = slideCount;
          this.context = context;
          this.snapsList = snaps;
      }
  
      @Override
      public Fragment getItem(int position) {
          FindSlideFragment fragment = new FindSlideFragment();
          if(!snapsList.isEmpty())
          fragment.setImage(snapsList.get(position).getImage());
          return fragment;
      }
  
      @Override
      public int getCount() {
          return slideCount;
      }
  
      public void setSlideCount(){
          slideCount = snapsList.size();
      }
  
      public void setData(ArrayList&lt;Snap&gt; snaps) {
          this.snapsList = snaps;
      }
  
  }
  }
  </code></pre>
  
  <p><strong>FindSlideFragment.java</strong></p>
  
  <pre><code>public class FindSlideFragment extends Fragment {
  
  
  ViewGroup rootView;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
  
      rootView = (ViewGroup) inflater.inflate(
              R.layout.find_content_fragment, container, false);
  
      return rootView;
  }
  
  
  public void setImage(String image) {
      ImageView iv_snap = (ImageView)rootView.findViewById(R.id.imageView_snap);
      byte[] imageAsBytes = Base64.decode(image, Base64.DEFAULT);
      Bitmap bmp = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
      iv_snap.setImageBitmap(bmp);
  }
    }
  </code></pre>
  
  <p><strong>StackTrace</strong></p>
  
  <pre><code>03-18 21:09:52.980 10354-10354/com.codelycan.foodtinder E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.codelycan.foodtinder, PID: 10354
                                                                        java.lang.NullPointerException
                                                                            at com.codelycan.foodtinder.FindSlideFragment.setImage(FindSlideFragment.java:39)
                                                                            at com.codelycan.foodtinder.FindFragment$ScreenSlidePagerAdapter.getItem(FindFragment.java:156)
                                                                            at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:105)
                                                                            at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:943)
                                                                            at android.support.v4.view.ViewPager.populate(ViewPager.java:1091)
                                                                            at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)
                                                                            at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
                                                                            at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:1019)
                                                                            at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2989)
                                                                            at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:279)
                                                                            at com.codelycan.foodtinder.FindFragment.reload(FindFragment.java:129)
                                                                            at com.codelycan.foodtinder.FindFragment.access$000(FindFragment.java:28)
                                                                            at com.codelycan.foodtinder.FindFragment$1.onChildAdded(FindFragment.java:64)
                                                                            at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:48)
                                                                            at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
                                                                            at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
                                                                            at android.os.Handler.handleCallback(Handler.java:733)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:136)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5426)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:515)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                            at dalvik.system.NativeStart.main(Native Method)
  </code></pre>