question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NULL reference exception on takePicture()

See original GitHub issue

I only got this error on my Huawei Ascend P7 device and not on a Samsung Galaxy S5. When taking a picture, it crashes.

mTakePicture.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mCameraView.isCameraOpened()) {
            mCameraView.takePicture();
        }
    }
});

mCameraView.addCallback(new CameraView.Callback() {
    @Override
    public void onPictureTaken(CameraView cameraView, byte[] data) {
        super.onPictureTaken(cameraView, data);
        mListener.onPictureTaken(data); // Custom method to process the data
        mCameraView.stop();
        getActivity().getSupportFragmentManager().popBackStack();
    }
});

When deleting the following line it works, however I need to close the camera fragment. getActivity().getSupportFragmentManager().popBackStack();

This is the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.hardware.camera2.CameraCaptureSession.capture(android.hardware.camera2.CaptureRequest, android.hardware.camera2.CameraCaptureSession$CaptureCallback, android.os.Handler)' on a null object reference
    at com.google.android.cameraview.Camera2.unlockFocus(Camera2.java:625)
    at com.google.android.cameraview.Camera2.access$900(Camera2.java:45)
    at com.google.android.cameraview.Camera2$6.onCaptureCompleted(Camera2.java:609)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at android.hardware.camera2.dispatch.InvokeDispatcher.dispatch(InvokeDispatcher.java:39)
    at android.hardware.camera2.dispatch.HandlerDispatcher$1.run(HandlerDispatcher.java:65)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5569)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)

My guess is that I need to listen to another callback before killing the camera, but I couldn’t find one.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:11
  • Comments:11

github_iconTop GitHub Comments

4reactions
siokagamicommented, Jun 24, 2019

Just added

catch (NullPointerException n) {
            n.printStackTrace();
        }

to method unlockFocus() in Camera2.java class Dirty trick, but works.

Really work for me!!!!

4reactions
M0N3commented, Oct 11, 2018

Just added

catch (NullPointerException n) {
            n.printStackTrace();
        }

to method unlockFocus() in Camera2.java class Dirty trick, but works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

camera.takePicture null pointer exception - Stack Overflow
I think camera is null in your onClick() function because there is no guaranteed path in onCreate() to create the camera . Modify...
Read more >
CameraX crash on ImageCapture.takePhoto() [230454568]
getCameraInfoInternal()' on a null object reference ... DESCRIPTION: This is happening when takePicture() is called on the image capture.
Read more >
NullReferenceException Class (System) - Microsoft Learn
The exception that is thrown when there is an attempt to dereference a null object reference.
Read more >
CineMachine null reference exception on accessing brain ...
brain.ActiveVirtualCamera isn't valid at Awake() time. It only gets set after LateUpdate().
Read more >
image_picker | Flutter Package - Pub.dev
Handling MainActivity destruction on Android # ; void> getLostData() async ; await picker.retrieveLostData(); if ; if (response.files != null) ; final XFile file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found