Skip to content

Diagnostic reference

Exception: Argument cannot be null

Fix Exception: Argument cannot be null in Google Apps Script when passing null from getUserLabelByName, getSheetByName, or getCalendarById to a mutator.

Updated December 6, 2026

Apps Script error

Exception: Argument cannot be null: label

What it means

Apps Script lookup methods such as GmailApp.getUserLabelByName(), Spreadsheet.getSheetByName(), and CalendarApp.getCalendarById() return null instead of throwing when no match exists. Passing that result straight into thread.addLabel(), ss.deleteSheet(), or Utilities.formatDate() fails with Argument cannot be null.

Related error messages

  • Exception: Argument cannot be null: sheet
  • Exception: Argument cannot be null: date
  • Exception: Argument cannot be null: blob

Before and after

The code before and after the fix.

Problem 2 changed lines
function markThreadProcessed(thread) {
  // getUserLabelByName() returns null if "Processed" does not exist yet.
  thread.addLabel(GmailApp.getUserLabelByName('Processed'));
}
Fixed 3 changed lines
function markThreadProcessed(thread) {
  const label =
    GmailApp.getUserLabelByName('Processed') || GmailApp.createLabel('Processed');
  thread.addLabel(label);
}

How to fix it

  • For Gmail labels, fall back to creating the label: GmailApp.getUserLabelByName(name) || GmailApp.createLabel(name).
  • For sheets, assign const sheet = ss.getSheetByName(name) and check if (sheet) before calling ss.deleteSheet(sheet) or ss.setActiveSheet(sheet).
  • For Utilities.formatDate(date, tz, pattern), verify date instanceof Date && !Number.isNaN(date.getTime()) before formatting.

Things to check

  • Gmail label names are case-sensitive in getUserLabelByName() and use "/" for nested sub-labels (for example "Invoices/Paid").
  • myFunction flags direct getUserLabelByName() and getSheetByName() calls passed straight into addLabel, removeLabel, deleteLabel, deleteSheet, and setActiveSheet.

In the Apps Script editor

Catch errors like this before clicking Run.

myFunction (published as Error Checker for Google Apps Script on the Chrome Web Store) checks your code while you edit at script.google.com. Diagnostics, autocomplete, and rule-based quick fixes are free without an account, run in your browser, and request no Google Drive, Gmail, or Apps Script project OAuth scopes. Writing Apps Script with an AI coding agent? Connect the Apps Script MCP server to run the same static checks before pasting code.