Having trouble with TypeError

Having trouble with TypeError
0

#1

For some reason, my code is throwing a TypeError, and I cannot seem to grasp the problem.

HTML:

<!DOCTYPE html>
<html>
  <head>
    <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">-->
    <script src="popup.js" type="text/javascript"></script>
  </head>

  <body>
    <h1>MCPE Server Check</h1>
    <p>Server IP: <input id="ip-input"></p>
    <p>ecpehub.net</p>
    <p>19132</p>
    <p>Server Port: <input id="port-input"></p>
    <div class="results">
      <p id="server-ip"></p>
      <p id="server-port"></p>
      <p id="game"></p>
      <p id="online"></p>
      <p id="players"></p>
    </div>
    <button class="btn-info" id="btn">Go</button>
  </body>
</html>

JS:

var btn = document.getElementById("btn");
  var inputIP = document.getElementById("ip-input").value;
  var serverPort = document.getElementById("port-input").value;
  var reqURL = "https://use.gameapis.net/mcpe/query/extensive/" + inputIP + ":" + serverPort;


  btn.addEventListener('click', loadDoc);

  function loadDoc() {
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        var info = JSON.parse(xhttp.responseText);
        //console.log(info);
        document.getElementById("server-ip").innerHTML = info.hostname;
        document.getElementById("server-port").innerHTML = info.port;
        document.getElementById("game").innerHTML = info.game_name;
        document.getElementById("online").innerHTML = info.players.online;
        document.getElementById("players").innerHTML = info.players.max;
      }
    };
    xhttp.open("GET", reqURL, true);
    xhttp.send();
  }

For some reason I am getting a TypeError at var inputIP = document.getElementById("ip-input").value; saying “Cannot read property ‘value’ of null”.


#2

What data should I test the two input boxes with?


#3

The code throws a TypeError as soon as the HTML page is loaded.


#4

I do not get the TypeError until I click the Go button with the code you have posted. Do you have a Codepen with different code?