Call to a member function on null, although the constructor sets the object
Fatal error: Uncaught Error: Call to a member function query() on nullShort answer
The error means the variable in front of the arrow holds null at that moment. When you did set it in a constructor, the usual reasons are that a child class constructor skipped parent::__construct(), that the variable lives in another scope, or that a method returned null. Dump the variable right before the call to see which.
Fix it fast
1Find out which cause you have
Print the value just before the failing line. If it is null here, work back to where it should have been assigned.
var_dump($this->db); $this->db->query($sql);2Match what you see, then jump to its fix
- 1
The object is created in a parent class, and the child class has its own __construct()The child class constructor never calls the parent constructorFix - 2
A global variable is used inside a function or method, and PHP also warns Undefined variableThe variable is from another scope inside a functionFix - 3
The failing line chains calls such as $repo->find(5)->nameA method returned null and the result is used straight awayFix
- 1
What does the error look like?
Fatal error: Uncaught Error: Call to a member function query() on nullCall to a member function ... on null
Cause 1 of 3
The child class constructor never calls the parent constructor
1Check
The PHP manual says parent constructors are not called implicitly when the child defines a constructor.
grep -rn "function __construct" src/2Fix
Call parent::__construct() from the child constructor, with the arguments it needs.
class Child extends Base {
public function __construct(Db $db) {
parent::__construct($db);
}
}3Verify
Create the child object and call the method again.
php script.phpConfirmed by php.net
Cause 2 of 3
The variable is from another scope inside a function
1Check
The manual says variables created outside a function are not visible inside it unless declared global.
grep -n "global " file.php2Fix
Pass the object in as an argument or store it in a property and use $this. Use global only in legacy code.
function save(Db $db, $row) {
$db->insert($row);
}3Verify
Call the function with the object and confirm the error is gone.
php script.phpConfirmed by php.net
Cause 3 of 3
A method returned null and the result is used straight away
1Check
A lookup that finds nothing may return null, and the next arrow then fails. The manual notes that null is the expected value in such a case, which is when the nullsafe operator fits.
var_dump($repo->find(5));2Fix
Handle the null case, or use ?-> where null is an expected result (PHP 8.0 and later).
$user = $repo->find(5);
$name = $user?->name;3Verify
Run the line with an id that exists and one that does not.
php script.phpConfirmed by php.net
How do I stop it coming back?
- Use constructor promotion or typed properties so a missing value fails early and clearly.
- Call parent::__construct() in every child that defines its own constructor.
- Pass dependencies in as arguments instead of reaching for globals.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Fatal error: Call to a member function prepare() on null in PHP
Warning: Undefined variable $conn
The variable you call prepare() on holds null, so the database object was never available at that line.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix