"Call to a member function X() on null" in PHP — What It Means and How to Fix It
What the PHP error "Call to a member function X() on null" means and how to fix it.
Published October 31, 2026
You called a method on a variable that is null instead of an object. The object was never created or a lookup returned nothing.
What it means and why it happens
Typical causes: a database find() returning null, a missing dependency injection, a failed function that returns null, or using a property before it is set.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
Fatal error: Uncaught Error: Call to a member function getName() on null
Practical examples
Code that triggers it
<?php
$user = User::find(99); // returns null
echo $user->getName();The fix
<?php
$user = User::find(99);
if ($user === null) { throw new RuntimeException('User not found'); }
echo $user->getName();
// or: echo $user?->getName();Nullsafe operator
echo $order?->customer?->name ?? 'Unknown'; // PHP 8Common mistakes
- Not checking the result of lookups.
- Chaining calls without null checks.
- Forgetting to instantiate or inject the object.
Key takeaways
- Call to a member function on null: You called a method on a variable that is null instead of an object.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
What is ?->?
The nullsafe operator: returns null instead of throwing when the left side is null.
How do I find which line caused Call to a member function on null?
The error message and stack trace show the file and line; start there and work backwards.