This error means your code wrote $something->method() while $something held null. PHP cannot call a method on nothing, so it throws an Error. The method name in the message (format() here) is the call that failed; the variable that was null is on the same line of the file named in the stack trace.
PHP Fatal error: Uncaught Error: Call to a member function format() on null in /var/www/app/Report.php:41
Stack trace:
#0 /var/www/app/index.php(9): Report->render()
#1 {main}
thrown in /var/www/app/Report.php on line 41
The bug is rarely on the line PHP reports. That line only shows the first place the null was used. The real question is why the value was null.
Step 1: identify which expression is null
Open the file and line from the message. If the line chains calls, break it apart:
// Fails somewhere in the chain
echo $order->getCustomer()->getAddress()->getCity();
// Debug version
$customer = $order->getCustomer();
var_dump($customer); // null? this is your culprit
$address = $customer->getAddress();
Use var_dump() or a debugger such as Xdebug. A PHP 8 stack trace also tells you the caller, so you can check which argument supplied the null object.
Step 2: find why it was null
Typical reasons, most common first:
- A lookup returned nothing. A database fetch, repository
find(),array_pop()on an empty array,$stmt->fetch()with no row, or a Doctrine/Eloquent query for a record that does not exist. - The dependency was never set. A property declared but not assigned in the constructor, a setter not called, or a service container returning
nullfor an unregistered key. - Wrong scope or timing. A global such as
$wpdbor$postused outside the loop or before the hook where it exists. In WordPress,get_post(),get_term()andget_user_by()can returnnullorfalse, so a plugin that assumes an object breaks. - A typo in the variable name inside a function that cannot see the outer one.
- Failed parse or decode:
json_decode()returnsnullfor invalid JSON, andsimplexml_load_string()returnsfalse. Validate the payload with the JSON वैलिडेटर. - A real change in the data, such as a deleted user or a product that no longer has a category.
Step 3: choose the right fix
Do not just hide it. Decide what the correct behavior is when the object is missing.
Guard it explicitly:
$customer = $order->getCustomer();
if ($customer === null) {
return 'Guest'; // or throw a clear exception
}
return $customer->getName();
Use the nullsafe operator (PHP 8.0+) when null is an acceptable result:
$city = $order->getCustomer()?->getAddress()?->getCity(); // null if any link is null
$label = $city ?? 'Unknown';
The nullsafe operator short-circuits the whole chain, but it only suits cases where a missing value is normal. If a missing customer is a bug, it will hide it and move the failure somewhere else.
Fail early with a clear message where the value first becomes null. For example, make the lookup throw: findOrFail() in Laravel, or if (!$row) { throw new RuntimeException("Order $id not found"); }.
Type your code. A return type of ?Customer forces callers to handle the null case, and static analyzers such as PHPStan or Psalm flag the unguarded call before deployment.
Special case: WordPress and plugins
If this error appears after an update, the usual pattern is code that assumes a global exists. Deactivate the plugin named in the stack trace (rename its folder in wp-content/plugins over SFTP if the admin is down) and check whether a PHP or WordPress update changed behavior. The wider process is in WordPress plugin conflict troubleshooting और critical error on this website.
Verify
Reproduce the same request that failed, including the data that was missing (an old ID, an empty cart, a logged-out user). Add a test for that case. Then check the response with the HTTP स्टेटस चेकर: a fixed fatal error should turn a 500 into the expected 200 or a deliberate 404.
अगले कदम
- Read Fix 500 Internal Server Error.
- Compare with Uncaught TypeError, which appears when a wrong type is passed rather than null called.
- Review undefined array key warnings, a common source of null values.
- Add an uptime monitor with a keyword check on the affected page.
सामान्य प्रश्न
Why does PHP say "on null" and not name the variable?
The engine only knows the value's type at runtime, not what you called it. Use the file and line plus var_dump() to find the variable.
Does the ?-> operator fix the bug?
It prevents the crash, but if the object should exist, you now carry a null further down. Use it for optional data only.
Why did this start after a PHP upgrade?
PHP 8 turned several silent failures into exceptions and removed some old behavior. Code that relied on a lenient result may now get null where it used to get an object.
Is this the same as "on bool"?
Similar. A function returning false on failure gives "Call to a member function x() on bool". Check the return value before calling.
Get notified when this fix updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.