Android: canvas gives null pointer exception - How can fix it?
<p>I'm doing a joystick application where a button click should display a list view. However, when i open the list view the application shuts down with an error message about canvas having a null pointer exception and therefore cant get width/height of the canvas.
error:</p>

<pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Canvas.getWidth()' on a null object reference
</code></pre>

<p>the code:</p>

<pre><code>public class MySurfaceThread extends AsyncTask&lt;Void, Void, Void&gt;{

    SurfaceHolder mSurfaceHolder;
    CustomSurfaceView cSurfaceView;


    public MySurfaceThread(SurfaceHolder sh, CustomSurfaceView csv){
        mSurfaceHolder = sh;
        cSurfaceView = csv;
        x = y = 0;

        cSurfaceView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                x = event.getX();
                y = event.getY();
                System.out.println(x + " " + y);

                calculateValues(x,y);

                switch (event.getAction() &amp; MotionEvent.ACTION_MASK){
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_UP:
                        x = y = 0;
                        dx = dy = 0;
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        break;
                }
                return true;
            }

            private void calculateValues(float xx, float yy){
                dx = xx-zeroX;
                dy = yy-zeroY;
                angle = (float)Math.atan(Math.abs(dy / dx));
                c = (float)Math.sqrt(dx * dx + dy * dy);

                if(c &gt; radius){
                    if (dx &gt; 0 &amp;&amp; dy &gt; 0) { //lower right corner
                        xx = (float) (zeroX + radius * Math.cos(angle));
                        yy = (float) (zeroY + radius * Math.sin(angle));
                    }
                    else if(dx &gt; 0 &amp;&amp; dy &lt; 0){ //top right corner
                        xx = (float) (zeroX + radius * Math.cos(angle));
                        yy = (float) (zeroY - radius * Math.sin(angle));
                    }
                    else if(dx &lt; 0 &amp;&amp; dy &lt; 0){ //top left corner
                        xx = (float) (zeroX - radius * Math.cos(angle));
                        yy = (float) (zeroY - radius * Math.sin(angle));
                    }
                    else if(dx &lt; 0 &amp;&amp; dy &gt; 0){ //lower left corner
                        xx = (float) (zeroX - radius * Math.cos(angle));
                        yy = (float) (zeroY + radius * Math.sin(angle));
                    }
                }
                else {
                    xx = zeroX + dx;
                    yy = zeroY + dy;
                }
                System.out.println("dx: " + dx);
                System.out.println("dy: " + dy);
                x = xx;
                y = yy;
            }
        });


    }


    //@Override
    protected Void doInBackground(Void... params) {


        while(run) {



            Canvas canvas = new Canvas();
            canvas.drawBitmap(background,0,0,null);
            cSurfaceView.surfaceCreated(mSurfaceHolder);



            try {

                canvas = mSurfaceHolder.lockCanvas(null);

                synchronized (mSurfaceHolder) {

                    zeroX = (1704-1704/8);
                    zeroY = (915-915/4);
                    cSurfaceView.onDraw(canvas, x, y, zeroX, zeroY);

                }
                Thread.sleep(10);
            } catch (InterruptedException e) {

            } finally {
                if (canvas != null) {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }

            }
        }
        return null;
    }
}
</code></pre>

<p>Also, if needed - heres the onDraw method:</p>

<pre><code> protected void onDraw(Canvas canvas, float x, float y, float zx, float zy){

    dx = x-zx;
    dy = y-zy;

    super.onDraw(canvas);

    canvas.drawRGB(255, 255, 255);
    canvas.drawBitmap(background, (canvas.getWidth() - canvas.getWidth() / 7) - background.getWidth() / 2, (canvas.getHeight() - canvas.getHeight() / 4) - background.getHeight() / 2, null);
    canvas.drawText(Float.toString(x), 60, 60, paint1);
    canvas.drawText(Float.toString(y), 60, 120, paint1);

    if (x == 0 &amp;&amp; y == 0) {
        canvas.drawBitmap(ball, (canvas.getWidth()-canvas.getWidth() / 7) - ball.getWidth() / 2, (canvas.getHeight()-canvas.getHeight() / 4) - ball.getHeight() / 2, null);

    }
    else {
        canvas.drawBitmap(ball, x - ball.getWidth() / 2, y - ball.getHeight() / 2, null);
    }
}
</code></pre>

<p>Do you guys have any idea how i can initialize the canvas in order for it to work?</p>

<p>Appreciate all the help i can get!</p>