Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There is an error in index.js,line 43 that say:
Uncaught TypeError: Cannot read property 'querySelector' of null





/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

var db = null;
var db2 = null;
var db3 = null;
var dbUser = null;
var dbName = "estudos.db";

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.receivedEvent('deviceready');
    },

    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);

        43 line--> var listeningElement = parentElement.querySelector('.listening');

        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');


        // OPERACOES BD - inicio

        //banco de dados local - aceite de termos e outras coisas
        dbUser = window.sqlitePlugin.openDatabase({name: 'user.db', location: 'default'});
        dbUser.transaction(function(tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS Users (flg_aceite, flg_valid_bd)');
        }, function(error) {
            alert('Transaction ERROR: ' + error.message);
        }, function() {
            console.log('Database OK');
        });

        //copia do banco de dados de estudos
        window.plugins.sqlDB.copy(dbName, 0, copysuccess, copyerror);
        // OPERACOES BD - fim
    }
};

app.initialize();

//---------------------------------------------------------------

function copysuccess()
{
    //primeira versão deste banco de dados. o comando anterior.
    //provavelmente realizou a cópia, abro o BD.
    db = window.sqlitePlugin.openDatabase({name: dbName});
    //preciso verificar se existem versões anteriores deste BD. Deleto por precaucao
    dropTable();
    fts_table();
}

function copyerror(e)
{
    //esta versao do banco de dados ja existe.
    //abro o BD
    db = window.sqlitePlugin.openDatabase({name: dbName});
    //db3 = window.sqlitePlugin.openDatabase({name: "vtestudos"});
    //alert("copyerror" + JSON.stringify(e));
}


//---------------------------------------------------------------

function fts_table(){
    db.transaction(function(tx) {
    tx.executeSql('CREATE VIRTUAL TABLE vtestudos USING FTS3(titulo, texto, id_titulo)', [], function(tx,res){
          //alert("nao deu erro");
          //db = window.sqlitePlugin.openDatabase({name: "vtestudos"});
          //alert("uai. deu pra abrir");

          db.transaction(function(tx) {
          tx.executeSql('INSERT INTO vtestudos(titulo, texto, id_titulo) SELECT titulo, texto, id_titulo FROM estudos', [], function(tx,res){
              //db3 = window.sqlitePlugin.openDatabase({name: "vtestudos"});
               console.log('insert ok');
          });
          }, function(err){
              alert(err.message);
          });

    });
    }, function(err){
        alert(err.message);
    });
}

//---------------------------------------------------------------

function dropTable()
{
    window.plugins.sqlDB.remove("estudosprev1", 0, rmsuccess,rmerror); 
    window.plugins.sqlDB.remove("estudosprev2", 0, rmsuccess,rmerror);  
}

function rmsuccess()
{
    //existe versão anterior
    //alert("removesuccess");
    console.log('existe versão anterior');
}

function rmerror(e)
{
    //não existe versão anterior. ignoro.
    //alert("removeerror" + JSON.stringify(e));
    console.log('n existe versão anterior. ignoro.');
}

//---------------------------------------------------------------

/*
function displayNote(name)
{
db.transaction(function(tx) {
    tx.executeSql('SELECT * FROM estudos', [], function(tx,res){
          alert(res.rows.item(0).titulo);
          //alert(res.rows.item(0).texto);

    });
}, function(err){
    alert(err.message);
    alert("An error occured while displaying the note");
});
}
*/


What I have tried:

I have tried to delete the line especified, but... The same error continue.
Posted
Updated 1-Sep-19 2:05am
v7
Comments
F-ES Sitecore 5-Apr-17 11:07am    
parentElement is null so whatever "id" is doesn't exist as an element id at the time your js code is running.

Quote:
Uncaught TypeError: Cannot read property 'querySelector' of null.

You get this error when you try to access a member (method/property) of an object which is null

Solution would be validating for null values before accessing the functions in it. but you will only have to trace the code to find why the object is null at the first place.

var parentElement = document.getElementById(id);
      if (parentElement != null) {
          var receivedElement = parentElement.querySelector('.received');
          listeningElement.setAttribute('style', 'display:none;');
      }
 
Share this answer
 
v2
Comments
MarkNopfler 5-Apr-17 12:17pm    
Alright.. Fixed a problem, but looks like that the error is biggest that it. It´s Have helped me too.Thanks.
Karthik_Mahalingam 6-Apr-17 3:47am    
cool, if it works, Please close this post. :)
As it says
Uncaught TypeError: Cannot read property 'querySelector' of null


 var parentElement = document.getElementById(id);
 
var receivedElement = parentElement.querySelector('.received');
 
listeningElement.setAttribute('style', 'display:none;');
// check for null before changing
if (receivedElement != null) receivedElement.setAttribute('style', 'display:block;');


Dito for listeningElement ofcorse
 
Share this answer
 
v2
Comments
MarkNopfler 5-Apr-17 12:30pm    
IT´s fixed a problem, but it show-me other. It´s look like an unusual error.But it´s already helped me with It issue.Thanks.
load .js file at the bottom of the page
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900