Fatal error: Call to a member function setTemplate() on a non-object

Fatal error: Call to a member function setTemplate() on a non-object

July 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Fatal error: Call to a member function setTemplate() on a non-object

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

You’re encountering an error when trying to call your custom block from footer.phtml file in Magento. The fatal error message indicates that you’re attempting to call the setTemplate() method on a non-object.

Understanding the Issue

The problem lies in how you’re creating and calling your custom block. Currently, you’re using the createBlock() function incorrectly when trying to set the template for your block inside footer.phtml file.

The Correct Approach: Setting Templates in Layout XML Files

Instead of setting the template directly while creating a block, it’s best practice to define templates in layout XML files and then use those definitions to create blocks. This approach ensures proper object creation and avoids null pointer exceptions.

Here’s how you can resolve your issue:

Step 1: Modify Your Custom Block Definition

First, let’s modify your Mycustomblock.php file to remove the unnecessary setTemplate() call in the constructor, as it will be defined in the layout XML later on.

class Monojit_Custom_Block_Mycustomblock extends Mage_Core_Block_Template
{
    public function _construct()
    {
        parent::_construct();
    }

    // ... rest of your code ...
}

Step 2: Create a Layout XML File

Create a new layout XML file local/Monojit/Custom/layout/mycustomlayout.xml with the following content:

<layout version="1.0">
  <default>
    <reference name="header">
      <block type="custom/mycustomblock" template="custom/test.phtml"/>
    </reference>
  </default>
</layout>

This layout file tells Magento to create your Mycustomblock using the custom/test.phtml template inside the header block.

Step 3: Update Your Config XML

In your existing local/Monojit/Custom/etc/config.xml, add a <layout> node with a reference to the new layout XML file:

<config>
  <global>
    <!-- ... other configurations ... -->
    <layouts>
      <mycustom_layout translate="label" module="custom">
        <label>Custom Layout</label>
        <file>mycustomlayout.xml</file>
      </mycustom_layout>
    </layouts>
    <!-- ... rest of your configurations ... -->
  </global>
</config>

Step 4: Call the Block in Footer.phtml

Finally, update your footer.phtml file to call the block using its identifier:

<?php echo $this->getChildHtml('mycustom_block_identifier'); ?>

Replace 'mycustom_block_identifier' with a unique identifier for your block.

Step 5: Refresh Magento’s Configuration

After making these changes, flush Magento’s configuration cache to ensure the new settings take effect:

php bin/magento config:cache

Now, when you refresh your frontend page, the custom block should display correctly without throwing any errors. The layout XML file provides a proper definition for your block, ensuring it is created as an object before calling methods like setTemplate().

Remember to replace placeholders with your actual module and block names wherever necessary in the code examples above.

Conclusion

Calling blocks from footer.phtml files requires understanding how Magento’s block creation process works. By defining templates in layout XML files and using those definitions, you can avoid null pointer exceptions and ensure proper object creation before calling methods on your custom blocks.

For more detailed information on Magento customization and development best practices, feel free to explore the resources available at Laravel Company.

API Themes Configuration Errors

Our network