Having trouble with TypeError

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


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

    <h1>MCPE Server Check</h1>
    <p>Server IP: <input id="ip-input"></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>
    <button class="btn-info" id="btn">Go</button>


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);
        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);

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

What data should I test the two input boxes with?

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

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?