opentok-android-sdk-samples: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Handler.removeCallbacks(java.lang.Runnable)' on a null object reference

Describe the bug This is happening on production for us also. Device: Pixel 6 Android 13 Vonage SDK: 2.26

We see this occur when closing a previous session and creating a new close together. There seems to be no ‘good’ or recommended way to close a session other than nulling its reference and hoping that the garbage collector collects. That being said we are trying to use the life cycle methods to gracefully close the vonage session.

The main issue seems to be that vonage is attempting to remove something that does not exist or is already stopped.

The fact that I have to jump though so many hoops just to dispose and ensure vonage is not controlling the camera anymore is a bit crazy to me.

FATAL EXCEPTION: main
Process: com.transcarent.app, PID: 550
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Handler.removeCallbacks(java.lang.Runnable)' on a null object reference
	at com.opentok.android.Camera2VideoCapturer.stopDisplayOrientationCache(Unknown Source:4)
	at com.opentok.android.Camera2VideoCapturer.destroy(Unknown Source:11)
	at com.opentok.android.PublisherKit.a(Unknown Source:4)
	at com.opentok.android.PublisherKit.z(Unknown Source:0)
	at com.opentok.android.m0.run(Unknown Source:2)
	at android.os.Handler.handleCallback(Handler.java:942)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:201)
	at android.os.Looper.loop(Looper.java:288)
	at android.app.ActivityThread.main(ActivityThread.java:7918)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

It should be noted we have also tried to go directly to the capture of the publisher and stop that however stopping that results in a separate error.

Please give us a guaranteed way to COMPLETELY dispose of a session/publisher this is so frustrating.

I believe this error seems to be due to the SDK expecting a callback but because i removed it it is getting null. Is your SDK nullsafe on the java side when running callbacks that can be nulled?

To Reproduce If you run this code to close the previous sessions references x3-4 times you will run into the error i got.

// Fully connect to a session
// Leave. session

patientPublisher?.setPublisherListener(null)
patientPublisher?.setMuteListener(null)
patientPublisher?.setCameraListener(null)
patientPublisher?.setVideoListener(null)

session?.onPause()
patientPublisher?.onStop()

Expected behavior The session is closed without throwing errors.

Device (please compete the following information): Device: Pixel 6 Android 13 Vonage SDK: 2.26

Additional context Add any other context about the problem here.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 31 (15 by maintainers)

Most upvoted comments

@derekpitts28 @janoonaj @tinder-enginrumelioglu The safest way to stop the camera is, instead of calling directly onStop() method, use PublisherKit.setPublishVideo(boolean publishVideo) which internally will call onStop() method, it will stop publishing video (will send black frames to the subscriber) and will release the capturer. To stop it simply call publisher.setPublishVideo(false)

@goncalocostamendes I believe this does not turn on the camera and the system green light stays on because vonage holds onto the camera object. I don’t think this truely, stops/releases the camera and could cause users to think we are still using their camera when we are really not.

This is how we do it on our internal app, and I can validate that the onStop() method is called when publisher.setPublishVideo(false) is triggered. In addition, by calling onStop() directly instead of stop publishing video there may occur some errors, since you are trying to publish video that you are no longer capturing/rendering.

can confirm this worked for me. Thanks!

I am running into the same issues that Derek is running into. It should be rather easy to duplicate with the following steps;

  • Create a Publisher (no need to create a Session)
  • Render the View from the Publisher
  • Call Publisher.onStop and stop rendering the View when the activity is destroyed
  • Observe that the app crashes a few seconds later

@derekpitts28 it seems to me you will need to wait for the 2.27.1 release which provides more safety checks when stopping the capturer and associated threads. In the mean time I will try to replicate that scenario and check if any other improvement can be made

@goncalocostamendes just an FYI for your suggestion for (2) above I cannot find a way to verify if onStopped was called it does not look like there is a public getter for isStopped as it is package private for the BaseVideoCapturer. If this is to be the recommended way of closing this, it would be great to have access to that state vs managing that ourselves.

Thanks, let me implement what you mentioned above and see if that fixes this on the devices that were crashing. I will get back to you once I have a result. Thanks again! 🚀

@derekpitts28 I can now have access to a mobile device with the same model. This bug will be worked on next sprint. I will provide you an update once I have one.

Please provide me the entire content of the function you call to end the session.

@derekpitts28, 2.26.1 should be out by mid-October.

Thanks so much @goncalocostamendes !! Do you by chance have an ETA for 2.26.1?

@derekpitts28

In a soon to be released v2.26.1 version (between this and the following week), we currectly terminate the camera capturer for all scenarios.

When combining the 2.26.1 version together with the correct way to gracefully terminate a session:

mPublisher.onStop();
mSession.unpublish(mPublisher);
mSession.disconnect();

I attached a video recording to the ticket reproducing the scenario with the new fixes: https://github.com/opentok/opentok-android-sdk-samples/assets/106695857/386057f6-36af-4fee-adbd-85b1282d8d78

Also, in regards to the object life-cycle concerns, I do not recommend nullifying anything. Resources will be automatically released by the garbage collector and all our native resources are released explicitly by the SDK (we override the finalize() method in order to do so).

I will close the ticket. If for some reason you find these changes do not solve your problem, please re-open it.