24

This is my Block File:

 <?php
 namespace ChennaiBox\Mymail\Block\Mail;
 class MailContent extends \Magento\Framework\View\Element\Template
 {
 protected $_objectManager;
 protected $customerSession;
 public function __construct(
 \Magento\Customer\Model\Session $customerSession, 
 \Magento\Framework\ObjectManagerInterface $objectManager
 ) {
 $this->customerSession = $customerSession;
 $this->_objectManager = $objectManager;
 }
 public function mymailData()
 {
try{
 if ($this->customerSession->isLoggedIn()) {
 $cutomerEmail =(string)$this->customerSession->getCustomer()->getEmail();
 echo $cutomerEmail;
 else{
 $this->_redirect('customer/account/login/');
 }
 }catch (Exception $e) {
 $e->getMessage();
 }
 }
 }

If I call this block I get error

PHP Fatal error: Call to a member function dispatch() on null in /var/www/html/magento2/vendor/magento/framework/View/Element/AbstractBlock.php on line 642, referer: http://magentodev.gworks.mobi/magento2/customer/account/index/

from apache error.log file., why, suggect me how to solve this problem.

Raphael at Digital Pianism
70.8k37 gold badges192 silver badges357 bronze badges
asked Apr 27, 2016 at 7:08

1 Answer 1

60

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

To fix that you need to update your constructor:

public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 \Magento\Customer\Model\Session $customerSession, 
 \Magento\Framework\ObjectManagerInterface $objectManager,
 array $data = []
 ) {
 parent::__construct($context, $data);
 $this->customerSession = $customerSession;
 $this->_objectManager = $objectManager;
 }

Don't forget to flush the var/cache and var/generation after your changes.

answered Apr 27, 2016 at 7:15
1
  • 2
    Thank you. This helped me with one of those 'I know I'm forgetting something but I can't remember what' situations. Commented Oct 2, 2018 at 16:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.