Why wont this loop? Getting - TypeError: document.getElementById(...) is null
<p>I am still learning JavaScript, so any use full advice is always welcome.
I am making a lottery simulator, Its all working well but.....
When I click the function to run, the random numbers are generated and comparisons made, however when I try to loop the function I get - TypeError: document.getElementById(...) is null.</p>

<p>I have tried a few things but think I am totally scratching my head with this.</p>

<p>The code in question is.</p>

<pre><code> var automateLottery = function(){
    numberOfWeeks = numberOfWeeks + 1;//tally up rounds
    //drawUniqueNumbers();

var p_01 = parseInt(document.getElementById("picked_number_01").value);
var p_02 = parseInt(document.getElementById("picked_number_02").value);
var p_03 = parseInt(document.getElementById("picked_number_03").value);
var p_04 = parseInt(document.getElementById("picked_number_04").value);
var p_05 = parseInt(document.getElementById("picked_number_05").value);
var p_06 = parseInt(document.getElementById("picked_number_06").value);

var r_01 = parseInt(document.getElementById("random_01").value);
var r_02 = parseInt(document.getElementById("random_02").value);
var r_03 = parseInt(document.getElementById("random_03").value);
var r_04 = parseInt(document.getElementById("random_04").value);
var r_05 = parseInt(document.getElementById("random_05").value);
var r_06 = parseInt(document.getElementById("random_06").value);


if(p_01===r_01||p_01===r_02||p_01===r_03||p_01===r_04||p_01===r_05||p_01=== r_06){
        document.getElementById("compare_01").value = ("You have matched number " + " " +p_01 );
        addUpMatches(1);
    }else{
        document.getElementById("compare_01").value = ("No matches" )
    };

if(p_02===r_01||p_02===r_02||p_02===r_03||p_02===r_04||p_02===r_05||p_02===r_06){
        document.getElementById("compare_02").value = ("You have matched number" + " " +p_02 );
        addUpMatches(1);
    }else{
        document.getElementById("compare_02").value = ("No matches" )
    };      

if(p_03===r_01||p_03===r_02||p_03===r_03||p_03===r_04||p_03===r_05||p_03===r_06){
        document.getElementById("compare_03").value = ("You have matched number" + " " +p_03 );
        addUpMatches(1);
    }else{
        document.getElementById("compare_03").value = ("No matches" )
    };

if(p_04===r_01||p_04===r_02||p_04===r_03||p_04===r_04||p_04===r_05||p_04===r_06){
        document.getElementById("compare_04").value = ("You have matched number" + " " +p_04 );
        addUpMatches(1);
    }else{
        document.getElementById("compare_04").value = ("No matches" )
    };

if(p_05===r_01||p_05===r_02||p_05===r_03||p_05===r_04||p_05===r_05||p_05===r_06){
        document.getElementById("compare_05").value = ("You have matched number" + " " +p_05);
        addUpMatches(1);
    }else{
        document.getElementById("compare_05").value = ("No matches" )
    };

if(p_06===r_01||p_06===r_02||p_06===r_03||p_06===r_04||p_06===r_05||p_06===r_06){
        document.getElementById("compare_06").value = ("You have matched number" + " " +p_06 );
        addUpMatches(1);
    }else{
        document.getElementById("compare_06").value = ("No matches" )
    };



declareMatches();//----Add up per round and total
    document.getElementById("amountOfMatched").value = ("You have matched" + " " +totalMatchedThisRound + " "+ "numbers"); 
    document.getElementById("weeks").value = ("You have played for" + " " +numberOfWeeks + " "+ "and have won £"+ totalWinnings );
    document.getElementById("amountOfThreeMatches").value = ("You have so far had" + " " +match3 + " "+ "matches of three"  );
    document.getElementById("amountOffourMatches").value = ("You have so far had" + " " +match4 + " "+ "matches of four"  );
    document.getElementById("amountOffiveMatches").value = ("You have so far had" + " " +match5 + " "+ "matches of five"  );
    document.getElementById("amountOfsixMatches").value = ("You have so far had" + " " +match6 + " "+ "matches of six"  );

    clearCount();//clears amount matched every round
    cleartempTotal();//clears total matched every round

 };

 for( var i = 0; i &lt; 10 ; i++){
    automateLottery();
 };
</code></pre>

<p>HTML:</p>

<pre><code>&lt;div id="random_numbers"&gt;
&lt;FORM NAME="random_numbs" ACTION="" METHOD="GET"&gt;

    &lt;INPUT TYPE="text" class="ran_numb" id="random_01" NAME="slot_01" VALUE=""&gt;
    &lt;INPUT TYPE="text" class="ran_numb" id="random_02" NAME="slot_02" VALUE=""&gt;
    &lt;INPUT TYPE="text" class="ran_numb" id="random_03" NAME="slot_03" VALUE=""&gt;
    &lt;INPUT TYPE="text" class="ran_numb" id="random_04" NAME="slot_04" VALUE=""&gt;
    &lt;INPUT TYPE="text" class="ran_numb" id="random_05" NAME="slot_05" VALUE=""&gt;
    &lt;INPUT TYPE="text" class="ran_numb" id="random_06" NAME="slot_06" VALUE=""&gt;
    &lt;br/&gt;&lt;br/&gt;
    &lt;INPUT TYPE="button" NAME="button2" Value="Draw Numbers"      onClick="drawUniqueNumbers()"&gt;
&lt;/FORM&gt;
</code></pre>

<p></p>

<pre><code> &lt;div id="matched_numbers"&gt;
&lt;FORM NAME="myform" ACTION="" METHOD="GET"&gt;

    &lt;INPUT TYPE="text"  NAME="matched_01" id="compare_01" VALUE=""&gt;&lt;br&gt;
    &lt;INPUT TYPE="text"  NAME="matched_02" id="compare_02" VALUE=""&gt;&lt;br&gt;
    &lt;INPUT TYPE="text"  NAME="matched_03" id="compare_03" VALUE=""&gt;&lt;br&gt;
    &lt;INPUT TYPE="text"  NAME="matched_04" id="compare_04" VALUE=""&gt;&lt;br&gt;
    &lt;INPUT TYPE="text"  NAME="matched_05" id="compare_05" VALUE=""&gt;&lt;br&gt;
    &lt;INPUT TYPE="text"  NAME="matched_06" id="compare_06" VALUE=""&gt;&lt;br&gt;
    &lt;br/&gt;
    &lt;INPUT TYPE="text"  NAME="numbers3" id="amountOfMatched" VALUE="You have matched 0 numbers"&gt;&lt;br&gt;
    &lt;br&gt;

    &lt;INPUT TYPE="text"  NAME="numbers4" id="amountOfThreeMatches" VALUE=""&gt;&lt;br&gt;
    &lt;INPUT TYPE="text"  NAME="numbers5" id="amountOffourMatches" VALUE=""&gt;&lt;br&gt;
    &lt;INPUT TYPE="text"  NAME="numbers6" id="amountOffiveMatches" VALUE=""&gt;&lt;br&gt;
    &lt;INPUT TYPE="text"  NAME="numbers6" id="amountOfsixMatches" VALUE=""&gt;&lt;br&gt;
    Stats&lt;br&gt;
    &lt;INPUT TYPE="text"  NAME="weeks"  id="weeks" VALUE=""&gt;&lt;br&gt;
&lt;/FORM&gt;
</code></pre>

<p></p>

<p>This is some of the html.</p>

<p>Just to confirm there are values going into the forms and variables.
I set up a button to trigger the function "automateLottery" which works fine.
It takes random numbers generated and compares them with numbers picked, then compiles the stats.
Trouble is when I set up automateLottery in a loop, it will not work.</p>

<p>Should I put up all my code here? Or is this enough?</p>

<p>Thanks again</p>

<p>Thanks in advance to anyone who can help.</p>
