title: Android fragment - null object reference
body: <p>I am trying to change the alpha on some images using a fragment in my main activity. However, everything I try I get the error:</p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method
    'android.view.View android.view.View.findViewById(int)' on a null
    object reference</p>
  </blockquote>
  
  <p>I call the public function from my main activity like :
  Main Activity:</p>
  
  <pre><code>public void decadeDotsFragment(){
  
                  DecadeDotsFragment fragment = new DecadeDotsFragment();
                  FragmentManager manager = getSupportFragmentManager();
                  manager.beginTransaction()
                          .replace(R.id.dots_fragment, fragment)
                          .addToBackStack(null)
                          .commit();
  
          fragment.selectDot(0);
  
      }
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>fragment:</p>
  
  <pre><code>public class DecadeDotsFragment extends Fragment {
  
      ImageView dot_0, dot_1, dot_2, dot_3;
      View rootView;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          rootView = inflater.inflate(R.layout.decade_dots_indicator_fragment, container, false);
  
          return rootView;
  
      }
  
      @Override
      public void onViewCreated(View view, Bundle savedInstanceState){
  
      }
  
      public void selectDot(int position){
  
          dot_0 = (ImageView) rootView.findViewById(R.id.dot_0);
          dot_1 = (ImageView) rootView.findViewById(R.id.dot_1);
          dot_2 = (ImageView) rootView.findViewById(R.id.dot_2);
          dot_3 = (ImageView) rootView.findViewById(R.id.dot_3);
  
          switch (position){
              case 0:
                  dot_0.setAlpha((float) 1);
                  dot_1.setAlpha((float) 0.5);
                  dot_2.setAlpha((float) 0.5);
                  dot_3.setAlpha((float) 0.5);
                  break;
              case 1:
                  dot_0.setAlpha((float) 0.5);
                  dot_1.setAlpha((float) 1);
                  dot_2.setAlpha((float) 0.5);
                  dot_3.setAlpha((float) 0.5);
                  break;
              case 2:
                  dot_0.setAlpha((float) 0.5);
                  dot_1.setAlpha((float) 0.5);
                  dot_2.setAlpha((float) 1);
                  dot_3.setAlpha((float) 0.5);
                  break;
              case 3:
                  dot_0.setAlpha((float) 0.5);
                  dot_1.setAlpha((float) 0.5);
                  dot_2.setAlpha((float) 0.5);
                  dot_3.setAlpha((float) 1);
                  break;
              default:
                  dot_0.setAlpha((float) 0.5);
                  dot_1.setAlpha((float) 0.5);
                  dot_2.setAlpha((float) 0.5);
                  dot_3.setAlpha((float) 0.5);
          }
  
      }
  
  
  }
  </code></pre>