Error Reference

Cannot read property of null: TypeError in script

Cannot read property of null: TypeError in script

Where this appears

This error appears in the System Log under System Diagnostics > Log > Application Log when server-side scripts attempt to access properties or methods on null objects. It commonly occurs in Business Rules, Script Includes, Scheduled Jobs, and other server-side scripting contexts during record operations or API calls.

Sourdough
Chrome Extension

Debugging this in a real instance?

Sourdough lets you trace "Cannot read property of null: TypeError in script" through system logs, script errors, and API health in the ServiceNow tab you are already using.

Add to Chrome

Free to install. Pro $5/month after a 14-day no-card trial.
Pro requires the ServiceNow admin role. Upgrade inside the extension.

Overview
Tasks
CMDB
API
Metrics
Monitor
Internals
Instance:sourdoughdev·Version:Yokohama
Instance StateONLINE
System StatusFully Operational
Session Timeout90 minutes
Logged-In Sessions2 (20 active)
Build Nameyokohama-12-18-2024_p1
IP Address10.159.128.43
Instance HealthHealth Score: 90%
🔥 5dSourdough (Chrome Plugin)Dark Mode

Cause

A GlideRecord query returned no results and the script tried to access properties on the empty record object without checking if it exists. (high likelihood)

Chained method calls where an intermediate method returned null or undefined, and subsequent methods were called on the null result. (high likelihood)

Attempting to access properties on function parameters or variables that were passed as null or undefined values. (medium likelihood)

Using dot notation to access nested object properties where a parent object in the chain is null or undefined. (medium likelihood)

Accessing form fields or client-side objects from server-side scripts where the objects don't exist in that context. (low likelihood)

Fix

Step 1: Navigate to System Diagnostics > Log > Application Log and locate the specific error entry to identify the exact script and line number causing the issue.

Step 2: Open the problematic script (Business Rule, Script Include, etc.) and locate the line where the property access is failing.

Step 3: Identify the object or variable that is null by examining the code structure and variable assignments leading up to the error line.

Step 4: For GlideRecord operations, add a .next() check or verify the record exists before accessing properties using if(gr.isValidRecord()) or if(gr.next()).

Step 5: For chained method calls, break them into separate lines with null checks between each step, or use optional chaining techniques where possible.

Step 6: Add defensive null checks using if(variableName != null && variableName != undefined) or if(gs.nil(variableName)) before accessing object properties.

Step 7: Test the modified script in a non-production environment to ensure the null checks work correctly and don't introduce new issues.

Step 8: Deploy the fix and monitor the System Log to confirm the TypeError no longer occurs during normal operations.

Test Your Knowledge

Quick 3-question quiz. See how your ServiceNow skills stack up.

Question 1 of 3Performance

A list view on a table with millions of records is slow. Best fix?

Select an answer to continue