title: getThreads() for Gmail returning NULL in Google Apps
body: <p>I'm trying to get the following Google Apps script to work, taken from another older Groups post. It is basically is working up to the point where the getThreads(n, m) is called. The error code coming back is:</p>
  
  <p>TypeError: Cannot call method "addToThreads" of null.</p>
  
  <p>Is there an easier way to debug the getThreads() function? Objective is to change the label from EVERNOTE to Scripts/Forward.</p>
  
  <pre><code>// Set up multiple labels/sub-labels and add one or more email addresses
  function forwardEmails() {
    forwardThreads("EVERNOTE", "username@m.evernote.com", "@Filtered Email Archive");
  //  forwardThreads("Forward/MoreItems", "username1@gmail.com, username2@gmail.com", "MORE INFO");
    }
  
  function forwardThreads(label, addr, subjSuffix) {
  
    var maxSubjLength = 250; 
    var applylabel = GmailApp.getUserLabelByName("Scripts/Forward");
  
    // Send individual and threaded emails.
    var msgs, msg, i, j, subject, options, labels, page;
    labels = GmailApp.getUserLabelByName(label)
    var threads = labels.getThreads()
    for (i=0; i &lt; threads.length; i++) {
      msgs = threads[i].getMessages();
      for (j=0; j &lt; msgs.length; j++) {
        msg = msgs[j];
  
        subject = msg.getSubject();
        if (subject.length + subjSuffix.length &gt; maxSubjLength) {
          subject = subject.substring(0, maxSubjLength - subjSuffix.length);
        }
  
        options = { htmlBody: msg.getBody(), attachments : msg.getAttachments() };
  
        GmailApp.sendEmail(addr, subject +" "+ subjSuffix, msg.getBody(), options);
  
      }
    }
  
    while(!page || page.length == 100) {
      page = labels.getThreads(0, 100);
  
    // Apply new label; move the thread out of other label
       applylabel.addToThreads(page);    // ***** Failing on this line - assume page is NULL
       labels.removeFromThreads(page);
        }  
  }
  </code></pre>