Fatal error: Uncaught Error: Call to a member function x() on null
Code called a method on a variable that is null instead of an object.
Seen on:
PHP
Meaning
The object you expected was never created or a lookup failed. It is PHP’s equivalent of a null pointer exception and is very common with ORMs (find() returns null) and DOM parsing.
Common causes
- Model lookup returned null (
User::find($id)) - Dependency not injected / service not initialised
- Method chain where an earlier call returned null
- Database connection object never created
⚡ Quick fix
- Check for null before calling:
if ($user) { … } - Use
findOrFail()in Laravel to get a 404 instead - Use the nullsafe operator
$user?->profile?->name(PHP 8)
Detailed fix by platform
PHP
- Laravel — replace
Model::find()withfindOrFail()when the record must exist
Code examples
Nullsafe operator
php
$city = $order?->customer?->address?->city ?? 'Unknown';How to diagnose
- Object — Which variable is null (the method name tells you)?
- Source — Where should it be created/fetched?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026