“Call to a member function get() on null” — we’ve assuredly come across this error before, but what does it mean, and how can you fix it?
Let’s first think about how this error occurs. A crucial aspect of the statement is the “on null” part.
This tells you, simply, that you are calling a function “on null”, meaning this:
...means that the function call of get()
is being called on an object ($item
) doesn't exist (is null
). This means that $item === null
.
PHP code example
Let’s look at an example of this in real life, using common errors that occur in Magento. However, this error could be thrown in any other PHP application as well, including Laravel, Drupal, etc.
Here’s a block of code from a PHTML template:
Let’s assume that executing this block of code returns the following error:
Call to a member function getCategoryId() on null
Why does this happen? Well, here’s what’s happening when the code passes through PHP’s parser:
The code tries to grab the category ID with the getCategoryId()
function, but since $product
is null
, it throws the error. The above snippet essentially evaluates to:
This means that the root of the error isn’t the call to getCategoryId()
, but rather that $product = null
.
Issues within looping code
This same issue can occur anywhere, including within iterable code, such as foreach
loops. Another common scenario is retrieving a collection of objects from the database, and then trying to retrieve a child object within an iteration of the loop.
Let’s look at another example, this time from within a loop:
And this returns the error:
Call to a member function getParentItem() on null
Again, when the call to the getParentItem()
function is executed, PHP bails out of the script, since the call to $shipmentItem->getOrderItem()
is evaluating to null
.
This means that we need to find out why getOrderItem()
is returning null
, so we need to inspect that function.
Stepping into the function
In the code we just looked at, we can step into the getOrderItem()
function to find out what is going on. In Magento, this will lead us to the method at Magento\Sales\Model\Order\Shipment\Item::getOrderItem()
.
This code checks if the _orderItem
property exists, and if it does not, it executes some code to grab the order from the database, using one of two conditions:
If a shipment is found on the ordered item, it executes the first line of the conditional. Otherwise, it falls back to attempting to load the order item from the order item ID, using the OrderItemFactory
class.
Set a breakpoint with Xdebug
Xdebug can be extremely helpful with identifying and resolving the error. Now that we’ve found the offending code, we can use Xdebug to confirm the issue.
Since we are concerned about the value of the _orderItem
property, we should set a breakpoint within this function and inspect the results of this property’s assignment. This will help to tell us which conditional is executing, so we can dig a bit deeper into that related code.
When executed, you will be able to clearly see that the first line which attempts to retrieve the order information is the culprit. Now, we can dig deeper into it, by stepping into that method.
Since we know that the order_item_id
is populated, we can step back out to the getItemById()
function and find out what is going on there.
And when we walk through this code, we can see that $this->getItems()
evaluates to null
. This means that this code is the culprit, and is the underlying issue at play.
Conclusion
In this situation, it appears that the lack of an items
property on the object is a data integrity error.
It’s possible that a migration of this order’s data failed, or that the order item data got corrupted. In any case, we found out why the attempt to grab the parent data with $shipmentItem->getOrderItem()->getParentItem()
failed, and it’s because the order item didn’t exist in the first place (or rather, is null
).
Hopefully this article has helped explain how a Call to a member function get() on null
error gets thrown, what goes on when the PHP code executes, and how to take steps to resolve the issue!
How you resolve it is up to you, but more often than not the issue is caused by missing data, or corrupt underlying data content. If it isn’t caused by loads out of the database, the most likely culprit is improper logic within the parent function which is calling the underlying method, which can also be resolved using a tool like Xdebug.
Next Steps
- Understand the Magento 2 Request-Response Lifecycle (Article)
- Start learning Magento with the Magento 2 Coding Jumpstart Course (Free, 13,000+ Students)
- Take the Magento 2 Coding Kickstart Course to truly learn Magento (700+ Students)