Magento : Call to a member function count() on a non-object

Magento : Call to a member function count() on a non-object

September 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento : Call to a member function count() on a non-object

Troubleshooting the “Call to a member function count() on a non-object” Error in Magento

If you’re working with a Magento store and suddenly encountering this cryptic error when adding subcategories, we’ll guide you through identifying and resolving it effectively.

The Underlying Issue

The error message Fatal error: Call to a member function count() on a non-object indicates that the method count() is being called on an object that isn’t actually an object. In your case, this happens in the drawImgItem function within Magento’s core navigation block.

Analyzing the Problematic Line

$children = $category->getChildren();
$hasChildren = $children && $children->count();  // <--- This is line 324 which causes error

The issue arises when $category->getChildren() returns null instead of an object. The subsequent call to $children->count() then fails because it’s trying to count a non-object.

Common Reasons for the Null Object

  1. Missing or Inactive Category: Ensure that the parent category exists and is active in your store’s admin panel. If it’s missing or inactive, getChildren() will return null.

  2. Incorrect Category ID: Check if you’re passing the correct category ID to the function. An invalid or non-existent ID can result in null being returned.

  3. Extension Conflicts: Sometimes, third-party extensions might interfere with Magento’s core functionality. Ensure that your installed extensions are compatible and up-to-date.

Resolving the Error

Option 1: Check for Null Before Counting

You can modify the problematic line to first check if $children is an object before calling count():

$children = $category->getChildren();
$hasChildren = ($children instanceof Mage_Catalog_Model_Category) ? $children->count() : 0;

This code checks if $children is an instance of Mage_Catalog_Model_Category. If it isn’t (i.e., if it’s null), the expression returns 0.

Option 2: Update Your Magento Version

If you’re running an older version of Magento, this issue may have been resolved in later releases. Upgrading to a newer stable version can often fix such core bugs.

For example, on Laravel Company, we’ve covered best practices for Magento upgrades that can help you stay current and secure.

Conclusion

The “Call to a member function count() on a non-object” error is typically caused by trying to call count() on an object that isn’t actually an object. In your case, this happened due to the parent category being missing or inactive in Magento’s admin panel.

By implementing the null check before calling $children->count(), you can ensure that your code gracefully handles cases where getChildren() returns null, preventing the fatal error from occurring.

Remember, staying updated with official documentation and following best practices for extension management can help prevent such issues. If you need further assistance or have questions about Magento development, feel free to explore our blog at Laravel Company for more in-depth guides and tutorials.

Happy coding!

Catalog Extensions Admin Errors

Our network