title: JavaScript null reference error
body: <p>I have a piece of code shown below that is supposed to select the first object in a list of image object. When it runs, about half of the time I am getting an error:</p>
  
  <pre><code>TypeError: Cannot read property of mylist
  </code></pre>
  
  <p>I realize that this is happening because it is trying to reference an object that has not yet been given a reference to an object. Basically, the page will load, and I want to call this function in JavaScript, however if this code runs before the list is referenced, it errors out. Here is my code snippet:</p>
  
  <pre><code>window.onload = function () {
         try {
              var tnl = $find('mylist');
  
  
              tnl.selectFirstItem();
         }
         catch (exception) {
              alert(exception);
         }
  }
  </code></pre>
  
  <p>The </p>
  
  <pre><code>tnl.selectFirstItem()
  </code></pre>
  
  <p>line is the one throwing the error to the catch block.</p>
  
  <p>So, my question is, is there a way to force it to wait until this object "tnl" has been referenced?</p>
  
  <p>Thanks!</p>