title: Uncaught TypeError: Cannot read property 'style' of null
body: <pre><code>var n1 = document.getElementById("n1").style["background-color"];
  </code></pre>
  
  <blockquote>
    <p><em>Uncaught TypeError: Cannot read property 'style' of null</em></p>
  </blockquote>
  
  <p>so I try to change a table cell's background color but I get that. Not only that, but it says cannot READ, but it can actually WRITE to it (I mean I can change the background color the same way but I can't read its value).</p>
  
  <p>CODE:</p>
  
  <pre><code>function checkDiagonal() {
      var n1 = document.getElementById("n1").style["background-color"];
      var n2 = document.getElementById("n2").style["background-color"];
      var n3 = document.getElementById("n3").style["background-color"];
      var n4 = document.getElementById("n4").style["background-color"];
      var n5 = document.getElementById("n5").style["background-color"];
      var n6 = document.getElementById("n6").style["background-color"];
      var n7 = document.getElementById("n7").style["background-color"];
      var n8 = document.getElementById("n8").style["background-color"];
      var n9 = document.getElementById("n9").style["background-color"];
      var n10 = document.getElementById("n10").style["background-color"];
      if (n1!=='000000' || n2!=='000000' || n3!=='000000' || n4!=='000000' || n5!=='000000' || n6!=='000000' || n7!=='000000' || n8!=='000000' || n9!=='000000' || n10!=='000000')
      {
          return;
      } else {
          var winner='Nobody';
          if (n1 == n2 &amp;&amp; n2 == n3) { winner=n3; }    
          if (n1 == n4 &amp;&amp; n4 == n7) { winner=n7; }    
          if (n1 == n5 &amp;&amp; n5 == n9) { winner=n9; }    
          if (n2 == n5 &amp;&amp; n5 == n8) { winner=n8; }    
          if (n3 == n5 &amp;&amp; n5 == n7) { winner=n7; }    
          if (n3 == n6 &amp;&amp; n6 == n9) { winner=n9; }    
          if (n4 == n5 &amp;&amp; n5 == n6) { winner=n6; }    
          if (n7 == n8 &amp;&amp; n8 == n9) { winner=n9; }
          if (winner=='Nobody') {
              document.getElementById("n1").style["background-color"]="FFFFFF";
              document.getElementById("n4").style["background-color"]="FFFFFF";
              document.getElementById("n7").style["background-color"]="FFFFFF";
              document.getElementById("n8").style["background-color"]="FFFFFF";
              document.getElementById("n9").style["background-color"]="FFFFFF";
          }
      }
  }
  var st = new playerTurnObj(); //Pointless to explain this, it has nothing to do
  &lt;/script&gt;
  &lt;/head&gt;&lt;body&gt;
  &lt;table align="center" border=0&gt;
  &lt;tr align="center"&gt;&lt;td id="n1" style="background-color:000000" onclick="switchTurnGen(st); mark('n1'); checkDiagonal('n1');"&gt;&lt;/td&gt;&lt;td id="n2" style="background-color:000000" onclick="switchTurnGen(st); mark('n2'); checkDiagonal('n2');"&gt;&lt;/td&gt;&lt;td id="n3" style="background-color:000000" onclick="switchTurnGen(st); mark('n3'); checkDiagonal('n3');"&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr align="center"&gt;&lt;td id="n4" style="background-color:000000" onclick="switchTurnGen(st); mark('n4'); checkDiagonal('n4');"&gt;&lt;/td&gt;&lt;td id="n5" style="background-color:000000" onclick="switchTurnGen(st); mark('n5'); checkDiagonal('n5');"&gt;&lt;/td&gt;&lt;td id="n6" style="background-color:000000" onclick="switchTurnGen(st); mark('n6'); checkDiagonal('n6');"&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr align="center"&gt;&lt;td id="n7" style="background-color:000000" onclick="switchTurnGen(st); mark('n7'); checkDiagonal('n7');"&gt;&lt;/td&gt;&lt;td id="n8" style="background-color:000000" onclick="switchTurnGen(st); mark('n8'); checkDiagonal('n8');"&gt;&lt;/td&gt;&lt;td id="n9" style="background-color:000000" onclick="switchTurnGen(st); mark('n9'); checkDiagonal('n9');"&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p><strong>EDIT</strong>: I found out the problem. the problem was at id n10, there are only ids until n9, so n10 obviously caused an error. Sorry for asking this then</p>