Can not set property 'innerHTML' of null

0

I tested the code below and it returned the error:

p1-rad1-RE_UP.html:35 Uncaught TypeError: Cannot set property 'innerHTML' of null
  at Object.listarTodos (p1-rad1-RE_UP.html:35)
  at p1-rad1-RE_UP.html:100
  p1-rad1-RE_UP.html:35 Uncaught TypeError: Cannot set property 'innerHTML' of null
  at Object.listarTodos (p1-rad1-RE_UP.html:35)
  at Object.add (p1-rad1-RE_UP.html:58)
  at HTMLFormElement.onsubmit (p1-rad1-RE_UP.html:7)

What can it be?

var cadastro = new function() {
  this.veiculos = [];
  this.el = document.getElementById("veiculos");
  this.itemEditar = -1;

  this.listarTodos = function() {
    var dados = '';

    if (this.veiculos.length > 0) {
      for (i = 0; i < this.veiculos.length; i++) {
        dados += '<tr>';
        dados += '<td>' + this.veiculos[i] + '</td>';
        dados += '<td><button onclick="cadastro.alterar(' + i + ')"> Alterar </button></td>';
        dados += '<td><button onclick="cadastro.excluir(' + i + ')"> Excluir </button></td>';
        dados += '</tr>';
      }
    }
    this.el.innerHTML = dados;
  };

  this.add = function() {
    var id;
    id++;
    var placa = document.getElementById("add-placa").value;
    var marca = document.getElementById("add-marca").value;
    var modelo = document.getElementById("add-modelo").value;

    if (placa && marca && modelo) {
      if (this.itemEditar == -1) {
        this.veiculos.push(id);
        this.veiculos.push(placa);
        this.veiculos.push(marca);
        this.veiculos.push(modelo);
      } else {
        this.veiculos.splice(this.itemEditar, 1, id);
        this.veiculos.splice(this.itemEditar, 2, placa.trim());
        this.veiculos.splice(this.itemEditar, 3, marca.trim());
        this.veiculos.splice(this.itemEditar, 4, modelo.trim());
        this.itemEditar = -1;
      }
      this.listarTodos();
    }

    this.novo();
  };

  this.editar = function(item) {
    this.itemEditar = item;
    var placa = this.veiculos[item];
    var marca = this.veiculos[item];
    var modelo = this.veiculos[item];
    if (placa && marca && modelo) {
      var elPlaca = document.getElementById("add-placa");
      var elMarca = document.getElementById("add-marca");
      var elModelo = document.getElementById("add-modelo");
      elPlaca.value = placa;
      elMarca.value = marca;
      elModelo.value = modelo;
    }
  };

  this.novo = function() {
    this.itemEditar = -1;
    var elId;
    var elMarca = document.getElementById("add-marca");
    var elPlaca = document.getElementById("add-placa");
    var elModelo = document.getElementById("add-modelo");

    elId.value = this.elId + 1;
    elMarca.value = '';
    elPlaca.value = '';
    elModelo.value = '';
  };

  this.excluir = function(item) {
    this.veiculos.splice(item, 1);
    this.listarTodos();
  };

}

function fechar() {
  document.getElementById("mostrar").style.display = 'none';
};
cadastro.listarTodos();
<html>

<head>
  <meta charset="utf-8">
  <title>RAD I</title>
</head>

<body>
  <form action="javascript:void(0);" method="POST" onsubmit="cadastro.add()">
    <input type="text" id="add-placa" placeholder="Placa">
    <input type="text" id="add-marca" placeholder="Marca">
    <input type="text" id="add-modelo" placeholder="Modelo">
    <input type="submit" value="Salvar">
    <p>
      <input type="text" id="add-placa" placeholder="Placa">
      <input type="submit" name="buscar" value="Buscar">
  </form>
</body>

</html>
    
asked by anonymous 20.04.2017 / 14:58

2 answers

2

Well, the error can not be more descriptive than this. You just need to know how to look for the problem.

See the error:

  

Can not set property ' innerHTML ' of null

Which part of the code tries to set the innerHTML property of something?

Fortunately, there is only one, here

this.el.innerHTML = dados;

So, where is this.el being initialized?

Here

this.el = document.getElementById("veiculos");

So the problem is that the return of getElementById is null .

Let's look for the element with this Id in HTML.

<form>
  <input id="add-placa">
  <input id="add-marca">
  <input id="add-modelo">
  <input>
  <p>
    <input id="add-placa">
    <input>
</form>

It does not. This is the error.

    
20.04.2017 / 15:09
0

Your code has some errors, in addition to trying to access elements that do not exist, such as "# veiculos", it is trying to access elements outside its scope.

Below is a redesigned version;

var Cadastro = function () {
  this.tabela = document.getElementById("veiculos");
  this.formulario = document.getElementById("formulario");
  this.addPlaca = document.getElementById("addPlaca");
  this.addMarca = document.getElementById("addMarca");
  this.addModelo = document.getElementById("addModelo");  
  this.salvar = document.getElementById("salvar");
  
  this.searchPlaca = document.getElementById("searchPlaca");
  this.buscar = document.getElementById("buscar");
  
  this.salvar.addEventListener("click", this.onSalvarClick.bind(this));
  this.buscar.addEventListener("click", this.onBuscarClick.bind(this));
  
  var template = document.getElementById("tmpl-veiculo").textContent;
  this.template = Handlebars.compile(template);
  this.Veiculos = [];
} 

Cadastro.prototype.onSalvarClick = function (event) {
  event.preventDefault();
  var isValid = this.formulario.checkValidity();
  if (!isValid) 
    return alert("Todos os Campos são obrigatorios");
  
  var veiculo = {};
  veiculo.Placa = this.addPlaca.value;
  veiculo.Marca = this.addMarca.value;
  veiculo.Modelo = this.addModelo.value;
  
  var tabela = document.createElement("table");
  tabela.innerHTML = this.template(veiculo);
  var linha = tabela.rows[0];  
  this.tabela.appendChild(linha);
  var veiculo = new Veiculo(this, linha);
  this.Veiculos.push(veiculo);
}

Cadastro.prototype.onBuscarClick = function (event) {
  event.preventDefault();
  var that = this;
  this.Veiculos.forEach(function (veiculo, indice) {
    var pattern = that.searchPlaca.value;
    var valor = veiculo.Placa.span.textContent;
    var isVisible = valor.indexOf(pattern) !== -1;
    veiculo.wrapper.classList.toggle("hide", !isVisible);
  });  
}

var Veiculo = function (cadastro, elem) {
  this.cadastro = cadastro;
  this.wrapper = elem;
  this.Placa = new Field(this, "Placa");
  this.Marca = new Field(this, "Marca");
  this.Modelo = new Field(this, "Modelo");
  
  this.editar = this.wrapper.querySelector(".editar");
  this.salvar = this.wrapper.querySelector(".salvar");
  this.excluir = this.wrapper.querySelector(".excluir");
  this.cancelar = this.wrapper.querySelector(".cancelar");
  
  this.editar.addEventListener("click", this.onEditarClick.bind(this));
  this.salvar.addEventListener("click", this.onSalvarClick.bind(this));
  this.excluir.addEventListener("click", this.onExcluirClick.bind(this));
  this.cancelar.addEventListener("click", this.onCancelarClick.bind(this));
}

Veiculo.prototype.onEditarClick = function (event) {
  this.wrapper.classList.add("edicao");
}

Veiculo.prototype.onSalvarClick = function (event) {
  this.Placa.span.textContent = this.Placa.input.value;
  this.Marca.span.textContent = this.Marca.input.value;
  this.Modelo.span.textContent = this.Modelo.input.value;
  this.wrapper.classList.remove("edicao");
}

Veiculo.prototype.onExcluirClick = function (event) {
  var index = this.cadastro.Veiculos.indexOf(this);
  this.cadastro.Veiculos.splice(index, 1);
  this.cadastro.tabela.removeChild(this.wrapper);
}

Veiculo.prototype.onCancelarClick = function (event) {
  this.Placa.input.value = this.Placa.span.textContent;
  this.Marca.input.value = this.Marca.span.textContent;
  this.Modelo.input.value = this.Modelo.span.textContent;
  this.wrapper.classList.remove("edicao");
}

var Field = function (veiculo, nome) {
  this.veiculo = veiculo;
  this.wrapper = this.veiculo.wrapper.querySelector("[data-field='" + nome + "']");
  this.span = this.wrapper.querySelector("span");
  this.input = this.wrapper.querySelector("input");
}

new Cadastro();
#veiculos tr.hide {
  display: none;
}

#veiculos tr span{
  display: block;
}

#veiculos tr input[type='text']{
  display: none
}

#veiculos tr input[type='button']:first-child{
  display: block;
}

#veiculos tr input[type='button']:last-child{
  display: none;
}

#veiculos tr.edicao span{
  display: none;
}

#veiculos tr.edicao input[type='text']{
  display: block
}

#veiculos tr.edicao input[type='button']:first-child{
  display: none;
}

#veiculos tr.edicao input[type='button']:last-child{
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.js"></script><html><body><formid="formulario" action="#" method="POST" >
    <input type="text" id="addPlaca" required placeholder="Placa">
    <input type="text" id="addMarca" required placeholder="Marca">
    <input type="text" id="addModelo" required placeholder="Modelo">
    <input type="submit" id="salvar" name="salvar" value="Salvar">
    <p>
      <input type="text" id="searchPlaca" placeholder="Placa">
      <input type="submit" id="buscar" name="buscar" value="Buscar">
  </form>
  <table>
    <thead>
      <tr>
        <th>Placa</th>
        <th>Marca</th>
        <th>Modelo</th>
        <th></th>
        <th></th>
      </tr>
    </thead>
    <tbody id="veiculos">
    </tbody>
  </table>
  <script id="tmpl-veiculo" type="text/x-handlebars-template">
  <tr>
    <td data-field="Placa">
      <span>{{Placa}}</span>
      <input type="text" value="{{Placa}}" />
    </td>
    <td data-field="Marca">
      <span>{{Marca}}</span>
      <input type="text" value="{{Marca}}" />
    </td>
    <td data-field="Modelo">
      <span>{{Modelo}}</span>
      <input type="text" value="{{Modelo}}" />
    </td>
    <td>
      <input type="button" class="editar" value="Editar" />
      <input type="button" class="salvar" value="Salvar" />
    </td>
    <td>
      <input type="button" class="excluir" value="Excluir" />
      <input type="button" class="cancelar" value="Cancelar" />
    </td>
  </tr>
  </script>
</body>

</html>
    
20.04.2017 / 16:15