Fatal error: Call to a member function on null
PHP Programming Language
Severity: CriticalWhat Does This Error Mean?
This error means you tried to call a method on a variable that is null — meaning it does not hold an object. For example: $user->getName() — if $user is null instead of a User object, PHP crashes with this error. This is one of the most common PHP errors. The fix is to check that the variable actually holds an object before calling methods on it.
Affected Models
- PHP 5.x
- PHP 7.x
- PHP 8.x
- All PHP versions
Common Causes
- A database query returned no results and the code tries to use the result as an object
- A function or method returned null instead of an object (possibly due to an error or missing data)
- The variable was never assigned a value — it was declared but never set to an object
- A factory method or constructor failed and returned null
- The variable was set to null earlier in the code (perhaps in a cleanup or reset block)
How to Fix It
-
Before calling any method on an object, check it is not null: if ($user !== null) { echo $user->getName(); } — or use a combined check: if ($user) {
In PHP, if ($object) evaluates to false if $object is null, false, 0, or an empty value. Use !== null for a strict null check.
-
In PHP 8.0 and later, use the null-safe operator (?->): echo $user?->getName() — this returns null instead of crashing if $user is null.
The null-safe operator is like optional chaining in JavaScript. It short-circuits the whole chain and returns null if any part is null.
-
Find out why the variable is null. Add var_dump($user) or error_log(var_export($user, true)) just before the failing line to inspect the value.
The real fix is usually upstream — the variable should have been set to a real object but was not.
-
If the variable comes from a database query, check whether the query returned results before using them.
Example with PDO: $row = $stmt->fetch(); if ($row) { echo $row['name']; } — fetch() returns false when there are no results, not an object.
-
If the variable comes from a function that might return null, handle that case explicitly: $result = getUser($id); if ($result === null) { return 'User not found'; }
Good PHP code handles the null case explicitly rather than hoping the value will always be set.
When to Call a Professional
This is always fixable yourself. The fix is to check if the variable is null before calling methods on it. Then figure out why it is null — the real fix is often upstream, where the value should have been set.
Frequently Asked Questions
How is this different from 'Call to undefined method'?
'Call to a member function on null' means the variable has no object at all (it is null). 'Call to undefined method' means the variable does hold an object, but the method you are trying to call does not exist on that object. Both crash your application — the first because there is no object, the second because the method is missing.
What does the null-safe operator (?->) do in PHP 8?
The null-safe operator lets you chain method calls safely. If any step returns null, the entire chain returns null instead of crashing. For example: $city = $user?->getAddress()?->getCity(); If $user is null, $city becomes null — no crash. If $user exists but getAddress() returns null, $city also becomes null — still no crash. This is a cleaner alternative to nested if ($x !== null) checks.
Should I use if ($obj) or if ($obj !== null)?
For null checks, if ($obj !== null) is more precise. if ($obj) also returns false for 0, false, empty string, and empty array — which can cause bugs if your variable could legitimately hold those values. For general existence checking of objects, if ($obj) is usually fine since a valid object is always truthy in PHP. Use !== null when you want to be explicit that you are only checking for null.