0

I'm developing a simple camera app which is based on the android developers tutorial but my I'm having some issues getting it running.

Here is my LogCat printout.

enter image description here

Now I have traced the NullPointerException to be at line 30 in CameraPreview as you have probably figured out to so the code for CameraPreview is below. I can code in Java and know that a NullPointer is when something is there which hasn't been created yet but as far as I can tell it has been.

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
private static final String TAG = "Preview";

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

    //Install callback to get notifications from the app
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //deprecated but required for Android Version 3.0 and under
}

public void surfaceCreated(SurfaceHolder holder) {
    //Surface been created now need to tell app where to draw PREVIEW
            try{
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            }
            catch(IOException e){
                Log.d(TAG, "Camera preview error: " + e.getMessage());
            }

}

Line 30 is the code snippet here - mCamera.setPreviewDisplay(holder);

From what I can tell is that there is an instance of mCamera in the 'global' variable private Camera mCamera.

Anyone got any ideas? Sorry its a long question formatted as best I could.

Thanks, Dan

P.S. I know there are other methods required in this class this is merely the 'problem snippet'.

4
  • 4
    post logcat errors.not an image Commented Aug 1, 2012 at 11:19
  • Waht about the place where you are calling that class?? Commented Aug 1, 2012 at 11:20
  • @Robert which class do you mean Camera? because that's imported from android.hardware.Camera.... Commented Aug 1, 2012 at 11:22
  • I believe you mCamera instance is null, check if it initializes properly. Commented Aug 1, 2012 at 11:23

4 Answers 4

1
public CameraPreview(Context context, Camera camera){
    super(context);
    mCamera = camera;

Looks like camera is getting passed on to CameraPreview as null.


Make sure you have these in your Manifest:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />  
<uses-feature android:name="android.hardware.camera.autofocus" /> 

Also, what's your getCameraInstance() method?

Sign up to request clarification or add additional context in comments.

24 Comments

@DanielD Post the method where you pass camera to CameraPreview and I'll have a look.
the only place camera and CameraPreview are is in my constructor?
@DanielD It's where you pass the variables to the CameraPreview, it has to be somewhere. (eg. CameraPreview cp = new CameraPreview(context, camera).
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera_demo); //get an instance of Camera mCamera = getCameraInstance(); // Create our Preview view and set it as the content of our activity. mPreview = new CameraPreview(this, mCamera); FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); preview.addView(mPreview); This is in CameraDemo.java is this what you mean?
@DanielD Yes, that's it. I'll have a look at it now.
|
1

Where is CameraPreview created?

Looks like new CameraPreview(context, null).

You have to be sure that CameraPreview get not a null-Camera.

4 Comments

surely CameraPreview is created in my constructor?
Post your line, what is the 2nd Argument for CameraPreview?
getCameraInstance() brings null. post this method.
does Camera.open() throws an exception?
0

Use Camera android.hardware.Camera.open() to initialize the camera when you start your activity.

2 Comments

where would that go in my code, the onCreate method in the main application class? i.e. CameraDemo?
where you instantiate the CameraPreview, pass the camera using that function.
-1

mCamera = camera.

at this line your 'camera' object could be null.... so check it first... like

if(camera==null){

Log.e("Camera","my camera is null...");

}

Try this.... at this way you can find the exact where the nullPointer occurs...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.