Magento – Fatal error: Call to a member function setData() on a non-object

magento-1.9

Fatal error: Call to a member function setData() on a non-object in
/opt/lampp/htdocs/magento192/app/code/core/Mage/Adminhtml/Block/Widget/Form/Container.php
on line 141.

I am trying to create custome edit for custome grid but strucking with above problem from 3 days.So please can any one give me solution for this and also give full code for edit grid if possible

controller :

<?php
class Caritor_Callmeback_Adminhtml_RequestController extends Mage_Adminhtml_Controller_Action {
    public function indexAction() {
        $this->loadLayout ();
        $this->_addContent ( $this->getLayout ()->createBlock ( 'callmeback/callmebackgrid_callmebackgrid', 'callmeback' ) );
        $this->renderLayout ();
    }
    public function newAction() {
        $this->_forward ( 'edit' );
    }

    public function editAction()
    {
        $id = $this->getRequest()->getParam('id', null);
        $model = Mage::getModel('callmeback/blogpost');
        if ($id) {
            $model->load((int) $id);
            if ($model->getId()) {
                $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
                if ($data) {
                    $model->setData($data)->setId($id);
                }
            } else {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('callmeback')->__('Example does not exist'));
                $this->_redirect('*/*/');
            }
        }
        Mage::register('callmeback_data', $model);

        $this->loadLayout();
        $this->_addContent ( $this->getLayout ()->createBlock ( 'callmeback/callmebackgrid_edit', 'callmeback' ) );
        $this->renderLayout();
    }

    public function saveAction()
    {
        if ($data = $this->getRequest()->getPost())
        {
            $model = Mage::getModel('callmeback/blogpost');
            $id = $this->getRequest()->getParam('id');
            if ($id) {
                $model->load($id);
            }
            $model->setData($data);

            Mage::getSingleton('adminhtml/session')->setFormData($data);
            try {
                if ($id) {
                    $model->setId($id);
                }
                $model->save();

                if (!$model->getId()) {
                    Mage::throwException(Mage::helper('callmeback')->__('Error saving example'));
                }

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('callmeback')->__('Example was successfully saved.'));
                Mage::getSingleton('adminhtml/session')->setFormData(false);

                // The following line decides if it is a "save" or "save and continue"
                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                } else {
                    $this->_redirect('*/*/');
                }

            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                if ($model && $model->getId()) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                } else {
                    $this->_redirect('*/*/');
                }
            }

            return;
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('callmeback')->__('No data found to save'));
        $this->_redirect('*/*/');
    }


    public function massDeleteAction() {
        $Ids = $this->getRequest ()->getParam ( 'callmeback' ); // $this->getMassactionBlock()->setFormFieldName('tax_id'); from Mage_Adminhtml_Block_Tax_Rate_Grid
        if (! is_array ( $Ids )) {
            Mage::getSingleton ( 'adminhtml/session' )->addError ( Mage::helper ( 'callmeback' )->__ ( 'Please select requests.' ) );
        } else {
            try {
                $rateModel = Mage::getModel ( 'callmeback/blogpost' );
                foreach ( $Ids as $Id ) {
                    $rateModel->load ( $Id )->delete ();
                }
                Mage::getSingleton ( 'adminhtml/session' )->addSuccess ( Mage::helper ( 'callmeback' )->__ ( 'Total of %d record(s) were deleted.', count ( $Ids ) ) );
            } catch ( Exception $e ) {
                Mage::getSingleton ( 'adminhtml/session' )->addError ( $e->getMessage () );
            }
        }

        $this->_redirect ( '*/*/index' );
    }
}

?>

Edit :

    $this->_objectId = 'blogpost_id';
    $this->_blockGroup = 'callmeback';
    $this->_controller = 'adminhtml_callmebackgrid';
    $this->_mode = 'edit';

    $this->_addButton('save_and_continue', array(
            'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'),
            'onclick' => 'saveAndContinueEdit()',
            'class' => 'save',
    ), -100);
    $this->_updateButton('save', 'label', Mage::helper('callmeback')->__('Save Example'));

    $this->_formScripts[] = "
        function toggleEditor() {
            if (tinyMCE.getInstanceById('form_content') == null) {
                tinyMCE.execCommand('mceAddControl', false, 'edit_form');
            } else {
                tinyMCE.execCommand('mceRemoveControl', false, 'edit_form');
            }
        }

        function saveAndContinueEdit(){
            editForm.submit($('edit_form').action+'back/edit/');
        }
    ";
    $form_block = $this->_blockGroup . '/' .
            $this->_controller . '_' .
            $this->_mode .
            '_form';
    echo $form_block;
}

public function getHeaderText()
{
    if (Mage::registry('callmeback_data') && Mage::registry('callmeback_data')->getId())
    {
        return Mage::helper('callmeback')->__('Edit Example "%s"', $this->htmlEscape(Mage::registry('callmeback_data')->getName()));
    } else {
        return Mage::helper('callmeback')->__('New Example');
    }
}
}

Form :

<?php
class Caritor_Callmeback_Block_Adminhtml_Callmebackgrid_Edit_Form extends Mage_Adminhtml_Block_Widget_Form {
    protected function _prepareForm()
    {
        if (Mage::getSingleton('adminhtml/session')->getExampleData())
        {
            $data = Mage::getSingleton('adminhtml/session')->getExamplelData();
            Mage::getSingleton('adminhtml/session')->getExampleData(null);
        }
        elseif (Mage::registry('callmeback_data'))
        {
            $data = Mage::registry('callmeback_data')->getData();
        }
        else
        {
            $data = array();
        }
        Mage::log("this is the form class");
        $form = new Varien_Data_Form(array(
                'id' => 'edit_form',
                'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
                'method' => 'post',
                'enctype' => 'multipart/form-data',
        ));

        $form->setUseContainer(true);

        $this->setForm($form);

        $fieldset = $form->addFieldset('callmeback_form', array(
                'legend' =>Mage::helper('bookshelf')->__('Example Information')
        ));

        $fieldset->addField('name', 'text', array(
                'label'     => Mage::helper('callmeback')->__('Name'),
                'class'     => 'required-entry',
                'required'  => true,
                'name'      => 'name',
                'note'     => Mage::helper('callmeback')->__('The name of the example.'),
        ));

        $fieldset->addField('email', 'text', array(
                'label'     => Mage::helper('callmeback')->__('Email'),
                'class'     => 'required-entry',
                'required'  => true,
                'name'      => 'email',
        ));

        $fieldset->addField('mobile', 'text', array(
                'label'     => Mage::helper('callmeback')->__('Mobile'),
                'class'     => 'required-entry',
                'required'  => true,
                'name'      => 'mobile',
        ));

        $form->setValues($data);

        return parent::_prepareForm();
    }
}

Best Answer

I had the same error in edit.php file

inside _construct() function

check your block name

$this->_blockGroup = 'your block group here';

hope this will work

Note:

$this->_blockGroup 

should hold block alias name that you set through config.xml.