title: Passing Assigned Value of a Spinner from Fragment to Fragment within Viewpager NullPointerException
body: <p>I've make it more simple and updated it based on the answers in my previous post: <a href="https://stackoverflow.com/questions/20649497/adding-assigned-values-in-spinner-nullpointerexception">Adding Assigned Values in Spinner NullPointerException</a></p>
  
  <p>I have a <strong>MainAcitivty</strong> that uses a <strong>ViewPager</strong>. I have 2 Fragments in my MainActivity (FragA and FragB)</p>
  
  <p>In my <strong>FragA</strong> I have a <strong>spinner</strong>. While in <strong>FragB</strong>, I have a <strong>TextView</strong> and a <strong>Button</strong>. </p>
  
  <p>Now what am I trying to do is, when I select "Hello" in my spinner, my int a will have a value of 5. And when I click the Button, 5 will display in the TextView.</p>
  
  <p>Here's my code:</p>
  
  <p><strong>FragA</strong></p>
  
  <pre><code>public class FragA extends Fragment {
  
      Spinner spinner1;
      String s1;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          View view = inflater.inflate(R.layout.fraga, container, false); 
  
          spinner1 = (Spinner) view.findViewById(R.id.spinner1);  
          ArrayAdapter&lt;CharSequence&gt;  adapter_a = ArrayAdapter.createFromResource(getActivity(), R.array.spinner1,android.R.layout.simple_spinner_item );
          spinner1.setAdapter(adapter_a);
  
          s1 = spinner1.getSelectedItem().toString();
  
          return view;
      }
  
      public int getInt() {
  
          int a = 0;
  
          if(s1.equals("Hello")) {
              a = 5;
          }
  
          return a;    
      }
  
  }
  </code></pre>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
      ViewPager viewPager = null; 
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          viewPager = (ViewPager)findViewById(R.id.pager);    
          FragmentManager fragmentManager = getSupportFragmentManager();
          viewPager.setAdapter(new MyAdapter(fragmentManager));
      }
  
      public class MyAdapter extends FragmentStatePagerAdapter {  
  
              public MyAdapter (FragmentManager fm) {
                  super(fm);
              }
  
              @Override
              public Fragment getItem(int i) {
                  Fragment fragment = null;
  
                  if (i == 0)
                  {
                      fragment = new FragA();
                  }
                  if (i == 1)
                  {
                      fragment = new FragB();
                  }
                  return fragment;
              }
  
              @Override
              public int getCount() {
                  return 2;
              }   
          }
  
      public String get() {
  
          FragA FragA = new FragA();
  
          return Integer.toString(FragA.getInt());
      }
  }
  </code></pre>
  
  <p><strong>FragB</strong></p>
  
  <pre><code>public class FragB extends Fragment{
  
      TextView textView;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          View view = inflater.inflate(R.layout.fragb, container, false); 
  
          textView = (TextView) view.findViewById(R.id.textview);
          Button button = (Button) view.findViewById(R.id.button);
          button.setOnClickListener(Click);
  
          return view;
      }
  
      OnClickListener Click = new OnClickListener() { 
  
          @Override
          public void onClick(View v) {
  
              textView.setText(((MainActivity)getActivity()).get());          
          }
      };
  
  }
  </code></pre>
  
  <p>By the way, I have no problem passing the value of a from FragA to FragB when this is my code:</p>
  
  <pre><code>public int getInt() {
  
          int a = 5;
  
          return a;    
      }
  </code></pre>
  
  <p>But, it doesn't involve my spinner and that's not I want to do.</p>