[Java for Android App Development] Korvnisse:
Hello dear XDA Developers. i've begun making an app as a school project and i have come to a stop.

My app is making use of a custom camera to allow some extra functionallty that is needed. i followed the instructions on the "Custom camera" at the developer.android website

now when im trying to test the camera i get the error

"FATAL EXCEPTION: main

java.lang.NullPointerException"

11-19 14:36:56.500: E/AndroidRuntime(4502):	at com.example.MYAPP.CameraPreview.surfaceCreated(CameraPreview.java:35)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.view.SurfaceView.access$000(SurfaceView.java:81)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1820)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.view.Choreographer.doCallbacks(Choreographer.java:555)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.view.Choreographer.doFrame(Choreographer.java:525)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.os.Handler.handleCallback(Handler.java:615)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.os.Looper.loop(Looper.java:137)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at android.app.ActivityThread.main(ActivityThread.java:4931)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at java.lang.reflect.Method.invokeNative(Native Method)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at java.lang.reflect.Method.invoke(Method.java:511)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
11-19 14:36:56.500: E/AndroidRuntime(4502):	at dalvik.system.NativeStart.main(Native Method)

This is the code for the main camera activity

package com.example.MYAPP;

import com.example.MYAPP.R;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.widget.FrameLayout;

public class CameraActivity extends Activity {

private Camera mCamera;
private CameraPreview mCameraPreview;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.framelayout);

mCamera = getCameraInstance();
mCameraPreview = new CameraPreview(this, mCamera);

FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);

}

/**
* Helper method to access the camera returns null if it cannot get the
* camera or does not exist
*
* @return
*/
private Camera getCameraInstance() {
Camera camera = null;

try {
camera = Camera.open();
} catch (Exception e) {
// cannot get camera or does not exist
}
return camera;
}
}

and i use this code for the Camera Preview

package com.example.MYAPP;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements
SurfaceHolder.Callback {
private static final String TAG = null;
private SurfaceHolder mHolder;
private Camera mCamera;

@SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the
// preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.

if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}

// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}

// set preview size and make any resize, rotate or
// reformatting changes here

// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();

} catch (Exception e) {
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}

Any help with this problem is greatly appreciated!

//Also im sorry if this thread is in the wrong place, this is my first post on this forum!