title: Android SurfaceView in Custom Class
body: <p>So I keep running into a NullPointerException with a project I am doing in Android Studio. I need to get an image to show up in a custom class I'm calling DrawSurface. I just can't seem to find anything that relates to my issue. I've checked various previous questions and watched so many videos.</p>
  
  <p>The error I'm thrown looks like this:</p>
  
  <blockquote>
    <p>java.lang.NullPointerException   at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1269)   at android.graphics.Canvas.drawBitmap(Canvas.java:1325)   at edu.########.#####.cse.treasurehunter.###########.DrawSurface.onDraw_Original(DrawSurface.java:60)   at edu.########.#####.cse.treasurehunter.###########.DrawSurface.onDraw(DrawSurface.java:-1)</p>
  </blockquote>
  
  <p>My .xml looks like this:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity"&gt;
  &lt;edu.########.####.cse.treasurehunter.###########.DrawSurface android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/dsField"/&gt;
  &lt;LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="50dp"&gt;
      &lt;Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5" android:text="View Inventory" android:id="@+id/btnInventory"/&gt;
      &lt;Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5" android:text="Credits" android:id="@+id/btnCredits"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>And my DrawSurface class looks like this:</p>
  
  <pre><code>public class DrawSurface extends SurfaceView {
  private Bitmap mBMPField;
  private SurfaceHolder holder;
  
  public DrawSurface(Context context) {
      super(context);
      holder = getHolder();
      holder.addCallback(new SurfaceHolder.Callback() {
  
          @Override
          public void surfaceCreated(SurfaceHolder holder) {
  
          }
  
          @Override
          public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
              Canvas c = holder.lockCanvas();
              onDraw(c);
              holder.unlockCanvasAndPost(c);
          }
  
          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
  
          }
      });
      mBMPField = BitmapFactory.decodeResource(getResources(), R.drawable.field);
  }
  
  public DrawSurface(Context context, AttributeSet attrs) {
      super(context, attrs);
  }
  
  public DrawSurface(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
  }
  
  @Override
  protected void onDraw(Canvas canvas) {
      canvas.drawColor(Color.DKGRAY);
      canvas.drawBitmap(mBMPField, 100, 200, null);
  }
  </code></pre>
  
  <p>}</p>