title: Passing variable from Custom class to Fragment (Android)
body: <p>I'm currently working with android for my project and I can't figure out the way to passing variable from Custom class to Fragment. I can exchange variable between MainActivity and Fragment but I can't use the same way to do it in Custom class and Fragment.</p>
  
  <p>The way that I use to send variable between MainActivity and Fragment is something like this</p>
  
  <p>In Fragment file:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>public class FourthFragment extends Fragment {
  
      MainActivity activity;
      @Override
      public void onAttach(Context context) {
          super.onAttach(context);
          activity = (MainActivity) getActivity();
      }</code></pre>
  </div>
  </div>
  </p>
  
  <p>and after that I can use any methods from MainActivity in my Fragment.It's work fine! So I try to do the same way to passing variable from Custom class to Fragment but it's didn't work out. </p>
  
  <p>The code that I tried.
  This is my Custom Class (MyAdapter.java)</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.ViewHolder&gt; {
          MainActivity activity;
  
  
  
          FourthFragment fourthfragment = new FourthFragment();
  
  
  
          private ArrayList&lt;CreateList&gt; galleryList;
              private Context context;
  
  
  
  
  
          public MyAdapter(Context context, ArrayList&lt;CreateList&gt; galleryList) {
                  this.galleryList = galleryList;
                  this.context = context;
              }
  
              @Override
              public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
                  View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_layout, viewGroup, false);
                  return new ViewHolder(view);
              }
  
  
  
  
          @Override
              public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, final int i) {
                  viewHolder.title.setText(galleryList.get(i).getImage_title());
                  viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
                  viewHolder.img.setImageResource((galleryList.get(i).getImage_ID()));
  
              viewHolder.img.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
                          builder1.setMessage("Are you sure you want to send this "+ galleryList.get(i).getImage_title());
                          builder1.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  Toast.makeText(context,"Yes",Toast.LENGTH_SHORT).show();
  
  
  
                                  //TODO: Problem here NULL pointer exception
  
  
                                  fourthfragment.imgSend();
  
                              }
                          });
                          builder1.setNegativeButton("No", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  Toast.makeText(context,"No",Toast.LENGTH_SHORT).show();
                              }
                          });
                          builder1.show();
                          Toast.makeText(context,"Image", Toast.LENGTH_SHORT).show();
                      }
                  });
              }
  
  
  
  
              @Override
              public int getItemCount() {
                  return galleryList.size();
              }
  
              public class ViewHolder extends RecyclerView.ViewHolder{
                  private TextView title;
                  private ImageView img;
                  public ViewHolder(View view) {
                      super(view);
  
                      title = (TextView)view.findViewById(R.id.title);
                      img = (ImageView) view.findViewById(R.id.img);
                  }
              }
          }</code></pre>
  </div>
  </div>
  </p>
  
  <p>And This one is my Fragment (FourthFragment.java) I only cut some parts of it that important.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>public class FourthFragment extends Fragment {
  
          MainActivity activity;
          MyAdapter myAdapter;
          RecyclerView recyclerView;
          byte[] dataOut2 = {(byte)255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
  
      @Override
          public void onAttach(Context context) {
              super.onAttach(context);
              activity = (MainActivity) getActivity();
          }
  
          @Override
          public void onCreate(@Nullable Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              init(savedInstanceState);
  
              if (savedInstanceState != null)
                  onRestoreInstanceState(savedInstanceState);
                }
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_fourth, container, false);
              initInstances(rootView, savedInstanceState);
              return rootView;
          }
  
          private void init(Bundle savedInstanceState) {
              // Init Fragment level's variable(s) here
          }
  
          @SuppressWarnings("UnusedParameters")
          private void initInstances(View rootView, Bundle savedInstanceState) {
              // Init 'View' instance(s) with rootView.findViewById here
              recyclerView = (RecyclerView) rootView.findViewById(R.id.imageGallery);
              recyclerView.setHasFixedSize(true);
              RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),2);
              recyclerView.setLayoutManager(layoutManager);
              ArrayList&lt;CreateList&gt; createLists = prepareData();
              MyAdapter adapter = new MyAdapter(getContext(), createLists);
              recyclerView.setAdapter(adapter);
  
          }
  
  
  
         //Below this is a method that I create to recieve variable(i) from Custom Class (MyAdapter.java)
  
          public void imgSend(int i)
          {
              if( i&lt; 28)
              {
                  dataOut2[8] = (byte)1;
                  int j = 0;
                  j = i + 1;
                  dataOut2[9] = (byte)j;
                  activity.sendMessage(dataOut2);
              }
              else if(i&gt;=28)
              {
                  dataOut2[8] = (byte)2;
                  int k = 0;
                  k = i - 27;
                  dataOut2[9]= (byte)k;
                  activity.sendMessage(dataOut2);
              }
              Toast.makeText(getContext(),""+dataOut2,Toast.LENGTH_SHORT).show();
          }</code></pre>
  </div>
  </div>
  </p>
  
  <p>And when I run all the program its caused a problem like this 
  when I start to click 'Yes' in Dialog that start to do a method
      fourthFragment.imgSend(i);</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>FATAL EXCEPTION: main
                                                                                        Process: myapplication.example.com.botcontrol, PID: 22397
                                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'void myapplication.example.com.botcontrol.MainActivity.sendMessage(byte[])' on a null object reference
                                                                                            at myapplication.example.com.botcontrol.Fragment.FourthFragment.imgSend(FourthFragment.java:156)
                                                                                            at myapplication.example.com.botcontrol.Fragment.MyAdapter$1$1.onClick(MyAdapter.java:70)
                                                                                            at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                            at android.os.Looper.loop(Looper.java:145)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:6939)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)</code></pre>
  </div>
  </div>
  </p>
  
  <p>So what can I do or any other way to send the variable? If anything else is needed, I will post it!
  Thank you</p>