简体   繁体   中英

Uncaught TypeError: Cannot set property 'innerHTML' of null in phonegap

I am trying a pass an array consisting of barcode scanned code in my barcode scannin app using phonegap plugins. I am getting an error "Uncaught TypeError: Cannot set property 'innerHTML' of null ". In the following code I have used a var k=0; and have declared an array var code= new Array(100); in the javascript file previously.

here is my html file

<!doctype html>
<html>
<head>
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
<script type="text/javascript" src="barcodescanner.js"></script>
  <script type="text/javascript" src="main.js"></script>
    </head>
   <body>
     <p />
      <p />
         <div  ng-controller="controller">
     <table ng-repeat="result in results">
                <tr>
                    <td>
                      S.No
                    </td>
                    <td >
                    Barcode
                    </td>

                </tr>
                <tr>
                    <td >
                       1
                    </td>
                    <td
                  id="d">
                  </td>                       
                </tr>
                <tr>
                    <td >
                       2
                    </td>
                    <td
                       id="code[0]">
                    </td>
                </tr>
                <tr>
                    <td >
                       3
                    </td>
                    <td
                        id="code[1]">
                    </td>
                    </tr>
        <tr><td><input class="butt" type="button" value="New Scan" onclick="scanCode();" /></td></tr>
            </body>
     <script type="text/javascript">
       var barcodeVal = localStorage.getItem("myvalue");
       document.getElementById("d").innerHTML = barcodeVal;
       </script>
    <script type="text/javascript">
        var scanCode = function () {
         window.plugins.barcodeScanner.scan(
           function (result) {

               alert("Scanned Code: " + result.text + ". Format: " + result.format + ". Cancelled: " + result.cancelled);
              localStorage.setItem("myvalue1", result.text);
              window.location.href = 'page5.html';

          }, function (error) {
              alert("Scan failed: " + error);
          });
  }
      </script>
      <script type="text/javascript">
      var barcodeVal = localStorage.getItem("myvalue1");
       document.getElementById("code[k]").innerHTML = barcodeVal;
        k=k+1;
      </script>
    </html>

In the following code i am tryin to scan barcode again and again and store the values in an array code[] . But i am getting the error. plz help me. How should i solve the following issue.I am using phonegap-1.4.1 and barcode scanning plugin for android.Thanks in advance.

Pease Change :

   <script type="text/javascript">

//space in bas codeVal--is wrong
          var bar codeVal = localStorage.getItem("myvalue1");
           document.getElementById("code[k]").innerHTML = barcodeVal;
            k=k+1;
          </script>

TO:

 <script type="text/javascript">
      var barcodeVal = localStorage.getItem("myvalue1");
      document.getElementById("code[0]").innerHTML = barcodeVal ;
       k=k+1;
   </script>

If your code is an array then use as below:

document.getElementById(code[index]).innerHTML = barcodeVal ;

change this

document.getElementById("code[k]").innerHTML = barcodeVal;

to

document.getElementById(code[k]).innerHTML = barcodeVal;

Because code is an array and you are trying to get element using its index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM