Skip to content

Null Pointer Dereference

Quick summary

You tried to read a property or call a method on null or undefined — the value has no properties to access.

Why this happens

  • A function returned null unexpectedly

  • An object was not initialized before use

  • A property access chain hit a null intermediate value

Minimal example

✗ Broken code
const user = null;
console.log(user.profile.name); // TypeError!
✓ Fixed code
const user = null;
console.log(user?.profile?.name ?? 'Unknown'); // 'Unknown'

user is null, so accessing user.profile throws a TypeError. Optional chaining (?.) returns undefined instead of throwing, and ?? provides a fallback value.

How to diagnose

  • Read the error message — it names the property that failed

  • Log the value before the property access to confirm it is null

  • Trace where the null value came from

How to fix

  • Add a null check before accessing properties

  • Use optional chaining (?.) to safely access nested properties

  • Provide a default value with the nullish coalescing operator (??)

How to prevent

  • Use optional chaining consistently for uncertain values

  • Initialize variables with default objects instead of null

Related Resources

Related Glossary

Related Lessons

  • Errors

    Learn about null-related errors.

← Back to language