Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do I have an error "TypeError: Cannot read properties of null (reading 'getTracks')", but the code is working?

Here is my code :

            const videoElem = document.getElementById("videoScan");
            var stream = document.getElementById("videoScan").srcObject;
            var tracks = stream.getTracks();
            tracks.forEach(function (track) {
                track.stop();
            });
            videoElem.srcObject = null;

I am trying to stop all camera streams in order to make ZXing work. However, I have the following error (only on android tablet) :

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Problem is, I can’t solve it, but everything is working well. And when I do a try/catch, code stops working. I can’t figure out why this error is displaying.

>Solution :

When you say "everything is working well", you mean that you get what you want as output?

If the code throwing the error is the same you are showing us, then it means that streams contains null.

Since streams is a const value (thus once only) assigned on the previous instructions, it means that document.getElementById("videoScan").srcObject is null too.

I’d suggest you to do some other debug by, i.e., printing out to JS console (if you have access to it from your Android debug environment, I’m sure there’s a way) what’s inside stream, before trying to access its getTracks method reference.

Please do also check the compatibility with srcObject with your Android (browser?) environment: MDN has a compatibility list you can inspect.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading