Magento – Getting error Call to a member function dispatch() on null in Block

magento2magento2.2.6

This is the proper way of outputting something

Fatal error: Uncaught Error: Call to a member function dispatch() on null in 
/opt/lampp/htdocs/learnm3/vendor/magento/framework/Model/ResourceModel/Db/Collection/AbstractCollection.php:531 Stack trace: #0 /opt/lampp/htdocs/learnm3/vendor/magento/framework/Data/Collection/AbstractDb.php(571): 
Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection->_beforeLoad() #1 /opt/lampp/htdocs/learnm3/vendor/magento/framework/Data/Collection/AbstractDb.php(559): Magento\Framework\Data\Collection\AbstractDb->loadWithFilter(false, false) #2 
/opt/lampp/htdocs/learnm3/vendor/magento/framework/Data/Collection.php(331): Magento\Framework\Data\Collection\AbstractDb->load() #3 
/opt/lampp/htdocs/learnm3/app/code/First/Module/Block/Index.php(22): Magento\Framework\Data\Collection->getItems() #4 
/opt/lampp/htdocs/learnm3/app/code/First/Module/view/frontend/templates/index.phtml(4): First\Module\Block\Index->gettingitem() #5 
/opt/lampp/htdocs/learnm3/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/opt/lampp/htd in 
/opt/lampp/htdocs/learnm3/vendor/magento/framework/Model/ResourceModel/Db/Collection/AbstractCollection.php on line 531

My block code is this

    <?php
namespace First\Module\Block;
use Magento\Framework\View\Element\Template;
use First\Module\Model\ResourceModel\Item\Collection ;
use First\Module\Model\ResourceModel\Item\CollectionFactory ;

class Index extends Template
{
    protected $cf ;
    public function __construct(
        Template\Context $context ,
        CollectionFactory $collectionfactory,
        array $data = []
    )
    {

        parent::__construct($context, $data);
        $this->cf = $collectionfactory;

    }
    public function gettingitem(){
        return $this->cf->create()->getItems();
    }

}

I am creating an extension (learning) I am stuck at that point.

I am asking first time for help on this platform so if I didn't follow the rules for asking a good quality question then ignore please thanks.

Best Answer

I think The problem is that your constructor does not match the parent class constructor.

Use this \Magento\Framework\View\Element\Template\Context $context, direct insted of use Magento\Framework\View\Element\Template

Try with below code. it might be helpful.

<?php
namespace First\Module\Block;    
use First\Module\Model\ResourceModel\Item\CollectionFactory ;

class Index extends Magento\Framework\View\Element\Template
{
protected $collectionfactory;
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    CollectionFactory $collectionfactory,
    array $data = []
)
{

    parent::__construct($context, $data);
    $this->collectionfactory = $collectionfactory;

}
public function gettingitem(){
    return $this->collectionfactory->create()->getItems();
}
}

And also try to replace class Post extends AbstractDb to class Post extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb. because you extends class from Magento\Framework\Data\Collection\AbstractDb.php

I hope it helps!