Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
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

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.

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

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.

about 4 years ago · Santiago Gelvez Relatório

0

The simplest way to catch this null reference error, is to check if tracks actually exists.

const videoElem = document.getElementById("videoScan");
const stream = videoElem.srcObject;

if (stream && stream?.getTracks()) {
  const tracks = stream.getTracks();
  tracks.forEach(function (track) {
    track.stop();
  });
} else {
  console.warn('Stream object is not available');
}

videoElem.srcObject = null;
about 4 years ago · Santiago Gelvez Relatório

0

So I found the solution. The error stopped the code from continuing, which, in my case, actually made it work. I made a very basic mistake : not paying enough attention to the debug results. However, if anyone has the error "Uncaught (in promise) DOMException: Could not start video source" with ZXing on Android, you simply have to cut all video streams to allow chrome starting an other camera. It will not work otherwise.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda