Null pointer exception in setImageBitmap
<p>I want to put a bitmap in a layout. I doing it by putting it in a image view..But the method setImageBitmap throws a null pointer exception...The setImageBitmap is in the sign method...I dont think the bitmap sign is null..Because it saves the signature image perfectly...So i am not getting what is the problem...please help???</p>

<pre><code>public class Signature extends View {
    public static Bitmap mBitmap;
    public static Canvas mCanvas;
    private Path mPath;
    public static Paint mBitmapPaint;
    public static Paint mPaint = null;
    private static final int BITMAP_HEIGHT = 800, BITMAP_WIDTH = 480;
    private Context ctx;
    public static Bitmap sign;
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
/**
 * Constructor used for initializing variable
 * 
 * @param c
 *            = Context of the application
 * @param attrs
 *            = AttributeSet of XML tags for signatureView
 */
public Signature(Context c, AttributeSet attrs) {

    super(c, attrs);
    ctx = c;
    initialize();

    // tv1.setText((CharSequence) tv);

}

private void initialize() {
    mBitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT,
            Bitmap.Config.ARGB_8888);// i have used 480X800 resolution
    mCanvas = new Canvas(mBitmap);
    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mPaint = new Paint();
    setpaint(Color.BLACK);
    mPaint.setColor(Color.BLACK);
    mPaint.setShadowLayer(10, color.darker_gray, 20, 20);
    mPaint.setTextSize(15);
    mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mCanvas.drawBitmap(mBitmap, 0, 0, null);
}

/**
 * This method sets the required parameters for paint object to be used.
 * This method also sets drawing cache for SignatureView. This method is
 * private for class and only called by constructor.
 * 
 * @param color
 *            = It takes the Color as an argument in integer form which is
 *            used while drawing.
 */
private void setpaint(int color) {
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(color);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(2);
    mPaint.setAlpha(255);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    this.setDrawingCacheEnabled(true);
    this.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
    this.setDrawingCacheQuality(DRAWING_CACHE_QUALITY_HIGH);
}

/**
 * This method clears canvas by reinitializing bitmap and canvas.
 */
public void clearCanvas() {
    // mBitmap = Bitmap.createBitmap(BITMAP_WIDTH,BITMAP_HEIGHT,
    // Bitmap.Config.ARGB_8888);
    // mCanvas=new Canvas(mBitmap);
    initialize();
    invalidate();
}

/**
 * This method enables saving drawing made on SignatureView.
 * 
 * @param file
 *            = This method takes a file object as parameter which is
 *            initialized with the path and filename to be saved.
 * @return = It returns a boolean value whether file is saved successfully
 *         or not.
 */
public boolean sign(File file) {
    sign = this.getDrawingCache();
    ImageView iv = (ImageView) findViewById(R.id.Ivsignature);
    try {
        iv.setImageBitmap(sign);

    } catch (Exception e) {
    }
    String filename = file.getAbsolutePath();
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        sign.compress(CompressFormat.JPEG, 100, fos);
        // Toast.makeText(ctx, filename+"  saved",
        // Toast.LENGTH_LONG).show();
        return true;
    } catch (Throwable ex) {
        Toast.makeText(ctx, "error: " + ex.getMessage(), Toast.LENGTH_LONG)
                .show();
        return false;
    }

}

/*
 * (non-Javadoc)
 * 
 * @see android.view.View#onSizeChanged(int, int, int, int)
 */
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

/*
 * (non-Javadoc)
 * 
 * @see android.view.View#onDraw(android.graphics.Canvas)
 */
@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath(mPath, mPaint);
}

/**
 * This method is called in onTouchEvent on ACTION_DOWN event this method
 * resets path to draw on canvas and reinitializes with new coordinates.
 * 
 * @param x
 *            = x coordinate of ACTION_DOWN event
 * @param y
 *            = y coordinate of ACTION_DOWN event
 */
private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

/**
 * This method is called in onTouchEvent on ACTION_MOVE event this method
 * adds coordinates to path for drawing on canvas.
 * 
 * @param x
 *            = x coordinate of while ACTION_MOVE event
 * @param y
 *            = y coordinate of while ACTION_MOVE event
 */
private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx &gt;= TOUCH_TOLERANCE || dy &gt;= TOUCH_TOLERANCE) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

/**
 * This method is called in onTouchEvent on ACTION_UP event this method
 * draws path on canvas and resets path.
 */
private void touch_up() {
    mPath.lineTo(mX, mY);
    mCanvas.drawPath(mPath, mPaint);
    mPath.reset();
}

/*
 * (non-Javadoc)
 * 
 * @see android.view.View#onTouchEvent(android.view.MotionEvent)
 */
@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        touch_start(x, y);
        mCanvas.drawPoint(x, y, mPaint);
        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        touch_move(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_UP:
        touch_up();
        invalidate();
        break;
    }

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

<p>}</p>

<pre><code>10-18 15:27:18.193: D/dalvikvm(480): GC_EXTERNAL_ALLOC freed 1453 objects / 87184 bytes in 175ms
10-18 15:27:25.333: D/dalvikvm(480): GC_EXTERNAL_ALLOC freed 836 objects / 56664 bytes in 130ms
10-18 15:27:25.763: D/dalvikvm(480): GC_EXTERNAL_ALLOC freed 208 objects / 12504 bytes in 112ms
10-18 15:27:30.592: D/AndroidRuntime(480): Shutting down VM
10-18 15:27:30.592: W/dalvikvm(480): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-18 15:27:30.622: E/AndroidRuntime(480): FATAL EXCEPTION: main
10-18 15:27:30.622: E/AndroidRuntime(480): java.lang.NullPointerException
10-18 15:27:30.622: E/AndroidRuntime(480):  at com.swipe_it.Signature.sign(Signature.java:117)
10-18 15:27:30.622: E/AndroidRuntime(480):  at com.swipe_it.LoadSignature$1.onClick(LoadSignature.java:93)
10-18 15:27:30.622: E/AndroidRuntime(480):  at android.view.View.performClick(View.java:2408)
10-18 15:27:30.622: E/AndroidRuntime(480):  at android.view.View$PerformClick.run(View.java:8816)
10-18 15:27:30.622: E/AndroidRuntime(480):  at android.os.Handler.handleCallback(Handler.java:587)
10-18 15:27:30.622: E/AndroidRuntime(480):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-18 15:27:30.622: E/AndroidRuntime(480):  at android.os.Looper.loop(Looper.java:123)
10-18 15:27:30.622: E/AndroidRuntime(480):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-18 15:27:30.622: E/AndroidRuntime(480):  at java.lang.reflect.Method.invokeNative(Native Method)
10-18 15:27:30.622: E/AndroidRuntime(480):  at java.lang.reflect.Method.invoke(Method.java:521)
10-18 15:27:30.622: E/AndroidRuntime(480):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-18 15:27:30.622: E/AndroidRuntime(480):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-18 15:27:30.622: E/AndroidRuntime(480):  at dalvik.system.NativeStart.main(Native Method)


&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" &gt;
&lt;TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Name"
    android:textColor="@android:color/black"
    android:textSize="25dp" /&gt;

&lt;TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="RRn"
    android:textColor="@android:color/black"
    android:textSize="25dp" /&gt;

&lt;TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Stan"
    android:textColor="@android:color/black"
    android:textSize="25dp" /&gt;

&lt;ImageView
    android:id="@+id/Ivsignature"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/&gt;
</code></pre>

<p></p>

<p>Can you please tell whats the problem?????</p>
