Save Image Canvas in sd card Java Null pointer Exception
<p>I am working on a application where i need to draw and save that as image in sd card </p>

<pre><code> public void onClickSave()
        {

            FrameLayout v = (FrameLayout) findViewById(R.id.main_frame);
            v.setDrawingCacheEnabled(true);

            v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            v.layout(0, 0, v.getWidth(), v.getHeight());
            v.buildDrawingCache(true);
            Bitmap bm = Bitmap.createBitmap(v.getDrawingCache());
            v.setDrawingCacheEnabled(false);
            if (bm != null) {
                try {
                    String path = Environment.getExternalStorageDirectory().toString();
                    OutputStream fOut = null;
                    File file = new File(path, "screentest.jpg");
                    fOut = new FileOutputStream(file);
                    bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                    fOut.flush();
                    fOut.close();                     
                }
                catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), e.toString(),1000).show();
                }
            }
</code></pre>

<p>and the function call occurs here</p>

<pre><code> Button btn_save=(Button) findViewById(R.id.button3);
        btn_save.setOnClickListener(new OnClickListener() 
        {

            public void onClick(View v) 
            {
                // TODO Auto-generated method stub

                drawView.onClickSave();

            }
        });
</code></pre>

<p>and i got the error in log as :</p>

<pre><code>05-23 12:46:25.413: W/dalvikvm(669): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-23 12:46:25.423: E/AndroidRuntime(669): FATAL EXCEPTION: main
05-23 12:46:25.423: E/AndroidRuntime(669): java.lang.NullPointerException
05-23 12:46:25.423: E/AndroidRuntime(669):  at com.page2.DrawView.onClickSave(DrawView.java:173)
05-23 12:46:25.423: E/AndroidRuntime(669):  at com.page2.Draw$3.onClick(Draw.java:60)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.view.View.performClick(View.java:2408)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.view.View$PerformClick.run(View.java:8816)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.os.Handler.handleCallback(Handler.java:587)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.os.Looper.loop(Looper.java:123)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-23 12:46:25.423: E/AndroidRuntime(669):  at java.lang.reflect.Method.invokeNative(Native Method)
05-23 12:46:25.423: E/AndroidRuntime(669):  at java.lang.reflect.Method.invoke(Method.java:521)
05-23 12:46:25.423: E/AndroidRuntime(669):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-23 12:46:25.423: E/AndroidRuntime(669):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-23 12:46:25.423: E/AndroidRuntime(669):  at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>and my xml is</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/white" &gt;
&lt;FrameLayout
 android:id="@+id/main_frame"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@android:color/white" android:layout_weight="0.1"&gt;

&lt;ImageView
     android:id="@+id/imageView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
      /&gt; 

&lt;/FrameLayout&gt;
&lt;RelativeLayout
 android:id="@+id/relativeLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
   android:layout_weight="0.9" android:layout_gravity="bottom" android:layout_marginTop="-10dp"&gt;
 &lt;Button
     android:id="@+id/button2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentTop="true"
     android:layout_centerHorizontal="true"
     android:text="Redo" /&gt;
 &lt;Button
     android:id="@+id/button1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"
     android:layout_alignParentTop="true"
     android:text="Undo" /&gt;
 &lt;Button
     android:id="@+id/button3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentRight="true"
     android:layout_alignParentTop="true"
     android:text="Save" /&gt;
 &lt;/RelativeLayout&gt;
&lt;/LinearLayout&gt;This is my xml 
</code></pre>

<p>Give me some other way of storing canvas draw as image in sd card
My app force closed when i clicked Save Button please help me
Thanks In Advance</p>
