Magento – Fatal error: Call to a member function load() on a non-object in IndexController.php

controllersdatabasemagento-1.9module

This Question is asked many times but still it's unresolved with definite solution. I am following this tutorial to implement the module http://devdocs.magento.com/guides/m1x/magefordev/mage-for-dev-5.html.

I am getting this error

Fatal error: Call to a member function load() on a non-object in C:\wamp\www\magentoapp\app\code\local\Mudit\Weblog\controllers\IndexController.php on line 9

while trying to fetch the data from the database table in the IndexController.php while others get this error in core files of magento abstract.php

Please, check the code for the config.xml, Blogpost.php and IndexController.php.

Please, help I am stuck with this from last 2 hours.

etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--
    The module's node contains basic
    information about each Magento module
-->
<modules>

    <!--
        This must exactly match the namespace and module's folder
        names, with directory separators replaced by underscores
    -->
    <Mudit_Weblog>

        <!-- The version of our module, starting at 0.0.1 -->
        <version>0.0.1</version>

    </Mudit_Weblog>

</modules>

<frontend>
    <routers>
        <weblog>
            <use>standard</use>
            <args>
                <module>Mudit_Weblog</module>
                <frontName>weblog</frontName>
            </args>
        </weblog>
    </routers>
</frontend>

<global>
    <models>

        <weblog>
            <class>Mudit_Weblog_Model</class>
            <!--
            need to create our own resource, can't just
            use core_resource
            -->
            <resourceModel>weblog_resource</resourceModel>
        </weblog>

        <weblog_resource>
            <class>Mudit_Weblog_Model_Resource</class>
            <entities>
                <blogpost>
                    <table>blog_posts</table>
                </blogpost>
            </entities>
        </weblog_resource>

    </models>
</global>

Model/Blogpost.php

class Mudit_Weblog_Model_Blogpost extends Mage_Core_Model_Abstract
{
protected function _construct()
{
$this->_init('weblog/blogpost');
}
}

Model/Resource/Blogpost.php

    <?php class Mudit_Weblog_Model_Resource_Blogpost extends   Mage_Core_Model_Resource_Db_Abstract
{
    public function _construct()
    {
    $this->_init('weblog/blogpost', 'blogpost_id');
    }
    }
    ?>

Controllers/IndexController.php

<?php class Mudit_Weblog_IndexController extends Mage_Core_Controller_Front_Action 
{

public function testModelAction() 
{
    $params = $this->getRequest()->getParams();
    $blogpost = Mage::getModel('weblog/blogpost');
    echo("Loading the blogpost with an ID of ".$params['id']);
    $blogpost->load($params['id']);
    $data = $blogpost->getData();
    var_dump($data);
}
}
?>

Best Answer

etc/modules/Mudit_Weblog.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Mudit_Weblog>
            <active>true</active>
            <codePool>local</codePool>
        </Mudit_Weblog>
    </modules>
</config>

Mudit/Weblog/controllers/IndexController.php

<?php 
class Mudit_Weblog_IndexController extends Mage_Core_Controller_Front_Action 
{
public function indexAction()
{
    echo "sdfds";
}
public function testModelAction() 
{
    $params = $this->getRequest()->getParams();
    $blogpost = Mage::getModel('weblog/blogpost');
            echo get_class($blogpost);

    echo("Loading the blogpost with an ID of ".$params['id']);
    $blogpost->load($params['id']);
    $data = $blogpost->getData();
    var_dump($data);
}
}
?>

Mudit/Web‌​log/Model/Blogpost.ph‌​p

class Mudit_Weblog_Model_Blogpost extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('weblog/blogpost');
    }
 }

Mudit/Web‌​log/Model/Resource/Blogpost.ph‌​p

<?php class Mudit_Weblog_Model_Resource_Blogpost extends   Mage_Core_Model_Resource_Db_Abstract
{
    public function _construct()
    {
    $this->_init('weblog/blogpost', 'blogpost_id');
    }
    }
    ?>

Mudit/Weblog/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>
    <modules>
        <Mudit_Weblog>
            <version>0.1.0</version>
        </Mudit_Weblog>
    </modules>
    <global>

        <models>
            <weblog>
                <class>Mudit_Weblog_Model</class>
                <resourceModel>weblog_resource</resourceModel>
            </weblog>
            <weblog_resource>
            <class>Mudit_Weblog_Model_Resource</class>
            <entities>
                <blogpost>
                    <table>blog_posts</table>
                </blogpost>
            </entities>
        </weblog_resource>
        </models>
    </global>

    <frontend>
        <routers>
            <weblog>
                <use>standard</use>
                <args>
                    <module>Mudit_Weblog</module>
                    <frontName>weblog</frontName>
                </args>
            </weblog>
        </routers>
    </frontend>

</config>