title: NullPointerException while setting image in a Fragment in Android
body: <p>I'm trying to set an image from a drawable inside an ImageView in a Fragment. The above code throws a <code>NullPointerException</code> at the line,</p>
  
  <blockquote>
    <p>imageView.setImageResource(R.drawable.featured_one);</p>
  </blockquote>
  
  <pre><code>public class FeaturedTwo extends Fragment {
  
      ImageView imageView;
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.featured_one, container, false);
  
          imageView = (ImageView) rootView.findViewById(R.id.featured_two_image);
  
          imageView.setImageResource(R.drawable.featured_one);
  
          return rootView;
  
      }
  }
  </code></pre>
  
  <p>Please help me find a solution.</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
                                                                                     at com.example.anandzoom.appytizertest.FeaturedTwo.onCreateView(FeaturedTwo.java:28)
                                                                                     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
                                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                                     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
  </code></pre>
  
  <p>XML of featured_one,</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;ImageView
          android:id="@+id/featured_one_image"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:scaleType="centerCrop" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>