Added null check 
Added null check 

    // in some exceptions the current thread will have no messages and messages.length will return Null, so 
    if (messages != null){
      for (var j = 0; j < messages.length; j++){
      ...
      }
    }
davidsuggitt/GAS-AUTOMATA