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.

cameraSource.release() throws null pointer exception if detector is null

See original GitHub issue

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.vision.Detector.release()' on a null object reference

According to [https://developers.google.com/android/reference/com/google/android/gms/vision/CameraSource.html#release()](Android Documentation) release method completely shuts down this camera source and releases the resources of the underlying detector. But sometimes detector is null and this method throws exception.

Would it not be better to handle camerasource and detector resource separately in that case?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
LucianIacobcommented, Feb 13, 2018

@Davehp44 we solved this in a way like:

if (mCameraSource != null) {
     try {
         mCameraSource.release();
     } catch (NullPointerException ignored) {  }
     mCameraSource = null;
}

And then for starting camera:

if (cameraSource == null) {
     releaseCamera();
     return;
}

mCameraSource = cameraSource;
mCameraSource.start(mSurfaceView.getHolder());
4reactions
vpdsouzacommented, Jun 2, 2017

Nope, I debugged the code to make sure the release is only called once and it is. But the moment I restart it I get this:

Exception thrown from receiver. java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.vision.Detector.receiveFrame(com.google.android.gms.vision.Frame)' on a null object reference at com.imbills.mobile.barcode.ui.camera.CameraSource$FrameProcessingRunnable.run(CameraSource.java:1205)

Read more comments on GitHub >

github_iconTop Results From Across the Web

After returning to activity CameraSource fires null exception
I moved the camera initialization code from onCreate to onResume and protected the camera release like this: mCameraView.getHolder().
Read more >
Java NullPointerException - Detect, Fix, and Best Practices
Invoking a method on an object instance but at runtime the object is null. Accessing variables of an object instance that is null...
Read more >
com.google.android.gms.vision.Detector java code examples
Releases the underlying receiver. This is only safe to do after the associated thread * has completed, which is managed in camera source's...
Read more >
Null Pointer Exception - C2 wiki
A Java Runtime Exception that signals an attempt to access a field or invoke a method of a null object. Note that the...
Read more >
https://academic.csuohio.edu/zhao_w/teaching/CIS69...
if (detector == null) { throw new IllegalArgumentException("No detector supplied. ... CAMERA) public CameraSource start() throws IOException { synchronized ...
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