title: Strange null pointer exception in Android Studios
body: <p>So while I was coding, I encountered something logically unexplainable.</p>
  
  <pre><code>public class Player {
  
      ImageView playerImage;
      Bitmap playerBmp;
      InputStream is;
      Drawable drawable;
  
      public void draw(){
  
          PlayActivity activity = new PlayActivity();
          playerImage = (ImageView) activity.findViewById(R.id.playerImage);
  
          is = activity.getResources().openRawResource(+R.drawable.main_character_drawing);
          playerBmp = BitmapFactory.decodeStream(is);
  
          drawable = new BitmapDrawable(activity.getResources(), playerBmp);
          playerImage.setImageDrawable(drawable);
      }
  }
  </code></pre>
  
  <p>And here is the PlayActivity class</p>
  
  <pre><code>public class PlayActivity extends Activity {
  
      Player player;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_play);
          player = new Player();
          player.draw();
  
      }
  }
  </code></pre>
  
  <p>And this is the XML for the activity_play</p>
  
  <pre><code>  &lt;ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/playerImage"
          android:layout_alignParentTop="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_marginTop="100dp"
          android:layout_marginLeft="250dp"/&gt;
  </code></pre>
  
  <p>However, when I run player.draw(), I get a null-pointer exception.
  Here is the error log :</p>
  
  <pre><code>07-07 17:49:05.291  31892-31892/com.example.testGame.henry. E/AndroidRuntime﹕ FATAL EXCEPTION: main
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testGame.henry./com.example.testGame.henry.PlayActivity}: java.lang.NullPointerException
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
              at android.app.ActivityThread.access$600(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4849)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.NullPointerException
              at android.app.Activity.findViewById(Activity.java:2045)
              at com.example.testGame.henry.Player.draw(Player.java:36)
              at com.example.testGame.henry.PlayActivity.onCreate(PlayActivity.java:26)
              at android.app.Activity.performCreate(Activity.java:5244)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
              at android.app.ActivityThread.access$600(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4849)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Long made short, the problem seems to be this line of code :</p>
  
  <pre><code>playerImage = (ImageView) activity.findViewById(R.id.playerImage);
  </code></pre>
  
  <p>Is there some sort of reason why this causes a null-pointer exception?
  I've also found that if I replaced the 
  <code>draw()</code> method with <code>draw(Activity activity)</code>, and then deleted the line</p>
  
  <pre><code>PlayActivity activity = new PlayActivity();
  </code></pre>
  
  <p>and instead simply wrote <code>player.draw(this)</code> (<code>this</code> being PlayActivity), it would run perfectly fine.</p>
  
  <p>No null-pointer exception, even though the solution I found is the exact same thing as creating an instance of PlayActivity. What is the cause for this?</p>
  
  <p>I don't understand why it's giving a null-pointer exception when I create and use an instance of PlayActivity, yet it doesn't when I pass PlayActivity into a method and use the parameter variable (which is PlayActivity).</p>