Android Nullpointer exception
<p>I am trying to record video and save to an external SD card. However, everytime I am getting three errors </p>

<p>anyone knew of any tutorials or could help correct my code.</p>

<p>could any one help me as i am getting this three errors</p>

<pre><code>    12-09 14:12:23.592: E/AndroidRuntime(4377): java.lang.NullPointerException
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at com.mahesh.VideoStreamActivity.surfaceChanged(VideoStreamActivity.java:71)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at android.view.SurfaceView.updateWindow(SurfaceView.java:568)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at android.view.SurfaceView.access$000(SurfaceView.java:82)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:171)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1590)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2382)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at android.os.Handler.dispatchMessage(Handler.java:99)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at android.os.Looper.loop(Looper.java:137)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at android.app.ActivityThread.main(ActivityThread.java:4340)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at java.lang.reflect.Method.invoke(Method.java:511)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    12-09 14:12:23.592: E/AndroidRuntime(4377):     at dalvik.system.NativeStart.main(Native Method)
    12-09 14:17:02.723: E/CAMERA_TUTORIAL(4596): /data/data/com.mahesh/cache/sdcard/videooutput: open failed: ENOENT (No such file or directory)

12-09 14:17:02.723: W/System.err(4596): java.io.FileNotFoundException: /data/data/com.mahesh/cache/sdcard/videooutput: open failed: ENOENT (No such file or directory)
12-09 14:17:02.734: W/System.err(4596):     at libcore.io.IoBridge.open(IoBridge.java:406)
12-09 14:17:02.734: W/System.err(4596):     at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:88)
12-09 14:17:02.734: W/System.err(4596):     at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:128)
12-09 14:17:02.734: W/System.err(4596):     at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:117)
12-09 14:17:02.734: W/System.err(4596):     at android.media.MediaRecorder.prepare(MediaRecorder.java:623)
12-09 14:17:02.734: W/System.err(4596):     at com.mahesh.VideoStreamActivity.startRecording(VideoStreamActivity.java:119)
12-09 14:17:02.734: W/System.err(4596):     at com.mahesh.VideoStreamActivity.surfaceCreated(VideoStreamActivity.java:50)
12-09 14:17:02.743: W/System.err(4596):     at android.view.SurfaceView.updateWindow(SurfaceView.java:562)
12-09 14:17:02.743: W/System.err(4596):     at android.view.SurfaceView.access$000(SurfaceView.java:82)
12-09 14:17:02.743: W/System.err(4596):     at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:171)
12-09 14:17:02.743: W/System.err(4596):     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590)
12-09 14:17:02.743: W/System.err(4596):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1590)
12-09 14:17:02.743: W/System.err(4596):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2382)
12-09 14:17:02.743: W/System.err(4596):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 14:17:02.743: W/System.err(4596):     at android.os.Looper.loop(Looper.java:137)
12-09 14:17:02.743: W/System.err(4596):     at android.app.ActivityThread.main(ActivityThread.java:4340)
12-09 14:17:02.753: W/System.err(4596):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 14:17:02.753: W/System.err(4596):     at java.lang.reflect.Method.invoke(Method.java:511)
12-09 14:17:02.753: W/System.err(4596):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-09 14:17:02.753: W/System.err(4596):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-09 14:17:02.753: W/System.err(4596):     at dalvik.system.NativeStart.main(Native Method)
12-09 14:17:02.753: W/System.err(4596): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
12-09 14:17:02.753: W/System.err(4596):     at libcore.io.Posix.open(Native Method)
12-09 14:17:02.753: W/System.err(4596):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:98)
12-09 14:17:02.763: W/System.err(4596):     at libcore.io.IoBridge.open(IoBridge.java:390)
12-09 14:17:02.763: W/System.err(4596):     ... 20 more
12-09 14:17:02.773: D/AndroidRuntime(4596): Shutting down VM
12-09 14:17:02.773: W/dalvikvm(4596): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
12-09 14:17:02.793: E/AndroidRuntime(4596): FATAL EXCEPTION: main
12-09 14:17:02.793: E/AndroidRuntime(4596): java.lang.RuntimeException: setParameters failed
12-09 14:17:02.793: E/AndroidRuntime(4596):     at android.hardware.Camera.native_setParameters(Native Method)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at android.hardware.Camera.setParameters(Camera.java:1253)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at com.mahesh.VideoStreamActivity.surfaceChanged(VideoStreamActivity.java:68)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at android.view.SurfaceView.updateWindow(SurfaceView.java:568)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at android.view.SurfaceView.access$000(SurfaceView.java:82)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:171)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1590)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2382)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at android.os.Looper.loop(Looper.java:137)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at android.app.ActivityThread.main(ActivityThread.java:4340)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at java.lang.reflect.Method.invoke(Method.java:511)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-09 14:17:02.793: E/AndroidRuntime(4596):     at dalvik.system.NativeStart.main(Native Method)

    code&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
    import java.io.File;
    import java.io.IOException;

    import android.app.Activity;
    import android.graphics.PixelFormat;
    import android.hardware.Camera;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.widget.Toast;

    public class VideoStreamActivity extends Activity implements SurfaceHolder.Callback{

        private static final String TAG = "CAMERA_TUTORIAL";

        private SurfaceView surfaceView;
        private SurfaceHolder surfaceHolder;
        private Camera camera;
        private boolean previewRunning;
        private MediaRecorder mediaRecorder;
        private final int maxDurationInMs = 20000;
        private final long maxFileSizeInBytes = 500000;
        private final int videoFramesPerSecond = 20;
        private File tempFile;



        String cacheFileName="/sdcard/videooutput";

            @Override
            public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.camera_surface);
                    surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
                    surfaceHolder = surfaceView.getHolder();
                    surfaceHolder.addCallback(this);
                    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            }

            @Override
        public void surfaceCreated(SurfaceHolder holder) {
            camera = Camera.open();
            if (camera != null){
                Camera.Parameters params = camera.getParameters();
                camera.setParameters(params);

            }
            else {
                Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
                finish();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            if (previewRunning){
                camera.stopPreview();
            }
            Camera.Parameters p = camera.getParameters();
            p.getPreferredPreviewSizeForVideo();
            p.setPreviewSize(width, height);

            p.setPreviewFormat(PixelFormat.JPEG);

            camera.setParameters(p);

            try {
                camera.setPreviewDisplay(holder);
                camera.startPreview();
                previewRunning = true;
            }
            catch (IOException e) {
                Log.e(TAG,e.getMessage());
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            camera.stopPreview();
            previewRunning = false;
            camera.release();
        }



        public boolean startRecording(){
            try {
                camera.unlock();

                mediaRecorder = new MediaRecorder();

                mediaRecorder.setCamera(camera);
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

                mediaRecorder.setMaxDuration(maxDurationInMs);


                tempFile = new File(getCacheDir(),cacheFileName);

                mediaRecorder.setOutputFile(tempFile.getPath());

                mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
                mediaRecorder.setVideoSize(surfaceView.getWidth(), surfaceView.getHeight());

                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

                mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

                mediaRecorder.setMaxFileSize(maxFileSizeInBytes);

                            mediaRecorder.prepare();
                mediaRecorder.start();

                return true;
            } catch (IllegalStateException e) {
                Log.e(TAG,e.getMessage());
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                Log.e(TAG,e.getMessage());
                e.printStackTrace();
                return false;
            }
        }

        public void stopRecording(){
            mediaRecorder.stop();
            camera.lock();
        }
    }
</code></pre>
