未捕获的类型错误:无法读取null的属性'queryselector' index.js [英] Uncaught type error: cannot read property 'queryselector' of null | index.js

查看:119
本文介绍了未捕获的类型错误:无法读取null的属性'queryselector' index.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

index.js第43行中有一个错误:

There is an error in index.js,line 43 that say:

Uncaught TypeError: Cannot read property 'querySelector' of null



< br $>











/*
 * 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.

推荐答案

正如它所说
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



Dito for listeningElement ofcorse


Quote:

Uncaught TypeError :无法读取属性'querySelector'为null。

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



当您尝试访问成员时出现此错误(code>对象的<方法/属性), 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;');
      }


这篇关于未捕获的类型错误:无法读取null的属性'queryselector' index.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆