Fix guides · Cursor, Lovable, Bolt, v0 ·
Fix Cannot read properties of null (reading 'X')
TypeError: Cannot read properties of null (reading 'X')
Your code reads a property or calls a method on a variable that is null at that moment. Unlike undefined, null is set on purpose: a function returned null, a query found no row, or data has not loaded yet (e.g. a fetch or async query that has not resolved). The fix is to find where the null comes from and handle that case before you read the property.
Common causes – and how to tell which one you have
1. Data not loaded yet (null before fetch resolves)
How to tell: The component reads a property on state/data immediately on first render, but the value comes from an async fetch, query, or effect that hasn't completed yet. The error happens on the first render and disappears once data loads. Stack trace points to the property access, and logging the value right before shows null.
Fix: Initialize state as null/undefined and render a loading or empty state until the data arrives. Use conditional rendering (if (!data) return <Loading />) or optional chaining only where null is expected during load. Move the property access inside the block that only runs after data is confirmed present.
2. A query returned no row
How to tell: Log the result. It shows null or an empty result where you expected an object.
Fix: Check for null before reading, and render an empty state.
3. A DOM element does not exist yet
How to tell: The stack trace points to a querySelector or getElementById line, for example .addEventListener or .value called on null.
Fix: Query the element after it is rendered (e.g. in useEffect), or check for null first.
4. A ref or state was never set
How to tell: The error happens on the first render, and the variable comes from useRef. React refs start as null until the element mounts.
Fix: Read the ref inside an effect, or use optional chaining while it is null.
5. A function returned null on an error path
How to tell: The variable comes from a function that has an early return null, such as a helper like getUser() when no session exists.
Fix: Handle the null return at the call site, or redirect to the login flow.
How to fix it
- Open the browser console and click the first stack trace line in your own code.
- Log the variable just before that line to confirm it is null.
- Find where the null is set and decide: is it expected (show an empty state) or a bug (fix the cause)?
- Add a null check before the property access, or use optional chaining (value?.name) only when null is a valid state.
- If the null is expected because data is loading (e.g., from a fetch or async query), show a loading indicator or empty state until the data arrives, and only read the property after the data is confirmed present.
Using Lovable, Bolt or v0?
Paste the full error and tell Lovable, Bolt or v0: "Find which property is read on null, log where that value is set, and fix why it is null. Don't just add ?. to hide it."
Using Cursor, Claude Code or Windsurf?
Ask Cursor or Claude Code to open the file and line from the top of the stack trace, log the variable just before it, and trace back to where it is set. Have it check the component both when the value is set and when it is null, and confirm nothing crashes.
The “fix” to avoid
Agents often add ?. or a fallback everywhere and call it fixed. That stops the crash but hides the real reason the value is null, so a failed query or a missing element now just shows nothing. Use a null check only where null is a valid state, and fix why the value is null.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My app crashes with "TypeError: Cannot read properties of null (reading 'X')". Use the stack trace to find the line that reads the property, then log the variable just before it. Find where that value is set and why it is null. Check in this order: 1) a query that returned no row, 2) a DOM element queried before it is rendered, 3) a ref read before its element mounts, 4) a function that returns null on an error path. Fix the cause, add explicit empty and error states, and don't just add ?. to hide the error. Then verify the screen both with data and when the value is null.
Questions people ask
Why null and not undefined?
null is assigned on purpose. undefined usually means a value was never set.
Is optional chaining a fix?
Only if null is a valid state. If the value should never be null, fix the cause.