# TypeError: Cannot read properties of null (reading 'Provider')

- **ID:** `react/usecontext-outside-provider-null`
- **Domain:** react
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

Attempting to destructure or access a property like Provider from a context object that is null, typically because the context was not created with createContext() or was imported incorrectly.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| React 16.8+ | active | — | — |
| React 17.0.2 | active | — | — |
| React 18.2.0 | active | — | — |

## Workarounds

1. **Ensure createContext is called and exported correctly. Example: export const MyContext = createContext(); then import { MyContext } from './context'; not import MyContext from './context'; if it's a named export.** (90% success)
   ```
   Ensure createContext is called and exported correctly. Example: export const MyContext = createContext(); then import { MyContext } from './context'; not import MyContext from './context'; if it's a named export.
   ```
2. **If using default export, change to: import MyContext from './context'; then use MyContext.Provider. Check that the export statement in the context file matches the import.** (85% success)
   ```
   If using default export, change to: import MyContext from './context'; then use MyContext.Provider. Check that the export statement in the context file matches the import.
   ```

## Dead Ends

- **Adding a null check before accessing Provider** — This masks the error but the context is still null, so the Provider component will be undefined and React will throw another error. (50% fail)
- **Using a default value for the context** — Default value only applies when consuming context with useContext, not when accessing Provider or Consumer from the context object itself. (60% fail)
- **Wrapping everything in a try-catch** — The error occurs at render time; try-catch won't catch it unless you use an error boundary, and it doesn't solve the root cause. (70% fail)
