title: Null pointer exception - Fragment Activity
body: <p>I'm fairly new to this and have managed to get the layout of my app and am using tabs and fragments.</p>
  
  <p>Each layout is a different calculator and requires different calculations to run. Im struggling to figure out how to do this.
  Any help is much appreciated.</p>
  
  <p>This first thing that i'm trying to get to work is the spinnersetup for the BMI fragment.
  Do I do this in the MainActivity or the Fragment? From what I have done so far I get a null pointer exception.</p>
  
  <p>Here is my MainActivity code:</p>
  
  <pre><code>public Fragment getItem(int position) {
              // getItem is called to instantiate the fragment for the given page.
              // Return a DummySectionFragment (defined as a static inner class
              // below) with the page number as its lone argument.
              switch (position) {
              case 0:
                  Fragment fragment = new DummySectionFragment();
                  Bundle args = new Bundle();
                  args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
                  fragment.setArguments(args);
                  return fragment;
  
                case 1:
                  Fragment fragment1 = new DummySectionFragment1();
                  Bundle args1 = new Bundle();
                  args1.putInt(DummySectionFragment1.ARG_SECTION_NUMBER, position + 2);
                  fragment1.setArguments(args1);
                  return fragment1;
  
                case 2:
  
                   Fragment fragment2 = new DummySectionFragment2();
                   Bundle args2 = new Bundle();
                   args2.putInt(DummySectionFragment2.ARG_SECTION_NUMBER, position + 3);
                   fragment2.setArguments(args2);
                   return fragment2;
  
                case 3:
  
                       Fragment fragment3 = new DummySectionFragment3();
                       Bundle args3 = new Bundle();
                       args3.putInt(DummySectionFragment3.ARG_SECTION_NUMBER, position + 4);
                       fragment3.setArguments(args3);
                       return fragment3;
  
              default:
                  return null;
              }
          }
  
          @Override
          public int getCount() {
              // Show 4 total pages.
              return 4;
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              Locale l = Locale.getDefault();
              switch (position) {
              case 0:
                  return getString(R.string.title_section1).toUpperCase(l);
              case 1:
                  return getString(R.string.title_section2).toUpperCase(l);
              case 2:
                  return getString(R.string.title_section3).toUpperCase(l);
              case 3:
                  return getString(R.string.title_section4).toUpperCase(l);
              }
              return null;
          }
      }
  
  }
  </code></pre>
  
  <p>My Fragment code:</p>
  
  <pre><code>package com.HealthKicks.bodystats.admob;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Spinner;
  
  public class DummySectionFragment extends Fragment {
  
      EditText height_edit_text1, weight_edit_text1, height, weight;
      Spinner height_spinner, weight_spinner;
      Button calculatebmi;
  
      public static final String ARG_SECTION_NUMBER = "section_number";
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.bmi, container, false);
  
          setupSpinners();
  
          height_spinner =(Spinner) getView().findViewById(R.id.height_spinner);
          weight_spinner =(Spinner) getView().findViewById(R.id.weight_spinner);
          height_edit_text1 =(EditText) getView().findViewById(R.id.height_edit_text1);
          weight_edit_text1 =(EditText) getView().findViewById(R.id.weight_edit_text1);
          weight_spinner =(Spinner) getView().findViewById(R.id.weight_spinner);
  
          return rootView;
      }
  
  
      void setupSpinners() {
  
          height_spinner =(Spinner) getView().findViewById(R.id.height_spinner);
          weight_spinner =(Spinner) getView().findViewById(R.id.weight_spinner);
          height_edit_text1 =(EditText) getView().findViewById(R.id.height_edit_text1);
          weight_edit_text1 =(EditText) getView().findViewById(R.id.weight_edit_text1);
  
          // TODO Auto-generated method stub
              height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                  if (position == 0){
                      height_edit_text1.setVisibility(View.GONE);
                  } else {
                      height_edit_text1.setVisibility(View.VISIBLE);
                  }
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
              }
          });
                  weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                      @Override
                      public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                          if (position == 0){
                              weight_edit_text1.setVisibility(View.GONE);
                          } 
                          else if (position == 1){
                              weight_edit_text1.setVisibility(View.GONE);
                          }
                          else {
                              weight_edit_text1.setVisibility(View.VISIBLE);
                          }
                      }
  
                      @Override
                      public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
                      }
                  });
      }   
  }
  </code></pre>
  
  <p>LogCat:</p>
  
  <pre><code>10-10 18:38:40.959: E/AndroidRuntime(1118): FATAL EXCEPTION: main
  10-10 18:38:40.959: E/AndroidRuntime(1118): java.lang.NullPointerException
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.HealthKicks.bodystats.admob.DummySectionFragment.setupSpinners(DummySectionFragment.java:33)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.HealthKicks.bodystats.admob.DummySectionFragment.onCreateView(DummySectionFragment.java:26)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.View.measure(View.java:15848)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.View.measure(View.java:15848)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.View.measure(View.java:15848)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.View.measure(View.java:15848)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.Choreographer.doFrame(Choreographer.java:532)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.os.Handler.handleCallback(Handler.java:730)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.os.Handler.dispatchMessage(Handler.java:92)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.os.Looper.loop(Looper.java:137)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at android.app.ActivityThread.main(ActivityThread.java:5103)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at java.lang.reflect.Method.invokeNative(Native Method)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at java.lang.reflect.Method.invoke(Method.java:525)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  10-10 18:38:40.959: E/AndroidRuntime(1118):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>