3

I got this error while saving image

Fatal error: Call to a member function create() on null in /opt/lampp/htdocs/magento/2.1/app/code/Contact/Modules/Controller/Contact/Index.php on line 85

here is my controller

<?php
/**
 *
 * Copyright © 2015 Contactcommerce. All rights reserved.
 */
namespace Contact\Modules\Controller\Contact;

class Index extends \Magento\Framework\App\Action\Action
{

    /**
     * @var \Magento\Framework\App\Cache\TypeListInterface
     */
    protected $_cacheTypeList;

    /**
     * @var \Magento\Framework\App\Cache\StateInterface
     */
    protected $_cacheState;

    /**
     * @var \Magento\Framework\App\Cache\Frontend\Pool
     */
    protected $_cacheFrontendPool;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;
    protected $_objectManager;
    protected $adapterFactory;
    protected $uploader;
    protected $filesystem;

    /**
     * @param Action\Context $context
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\Framework\App\Cache\StateInterface $cacheState
     * @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\ObjectManagerInterface $objectManager,
            \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
            \Magento\Framework\App\Cache\StateInterface $cacheState,
            \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory,
            \Magento\Framework\Image\AdapterFactory $adapterFactory,
            \Magento\MediaStorage\Model\File\UploaderFactory $uploader,
            \Magento\Framework\Filesystem $filesystem,

            array $data = []
    ) {

        parent::__construct($context, $data);
        $this->_objectManager = $objectManager;
         $this->_cacheTypeList = $cacheTypeList;
        $this->_cacheState = $cacheState;
        $this->_cacheFrontendPool = $cacheFrontendPool;
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     * Flush cache storage
     *
     */
    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
//        $email = $this->getRequest()->getParam('email');
//        $subject = $this->getRequest()->getParam('subject');
//        $message = $this->getRequest()->getParam('message');
//        $photo = $this->getRequest()->getParam('photo');
        if(isset($post))
        {
        $contact = $this->_objectManager->create('Contact\Modules\Model\Contact');
        $contact->setData('email', $post['email']);
        $contact->setData('subject', $post['subject']);
        $contact->setData('message', $post['message']);
        if (isset($_FILES['photo']) && isset($_FILES['photo']['name']) && strlen($_FILES['photo']['name']))
            {
            $base_media_path = 'Contact/Modules/images';
            $uploader = $this->uploader->create(
                ['fileId' => 'photo']
                );
                $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
                $imageAdapter = $this->adapterFactory->create();
                $uploader->addValidateCallback('photo', $imageAdapter, 'validateUploadFile');
                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(true);
                $mediaDirectory = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
                $result = $uploader->save(
                $mediaDirectory->getAbsolutePath(base_media_path));
        }
        $post['photo'] = base_media_path.$result['file'];
        $contact->setData('photo', $post['photo']);
        $contact->save();
        }
        else
        {
        $this->resultPage = $this->resultPageFactory->create();  
    return $this->resultPage;
        }
    }
}

Please suggest me solution of this error..

2 Answers 2

5

You forgot to declare some protected variables, you need to add the following

protected $uploader;
protected $adapterFactory;
protected $filesystem;

And also add the following to your __construct() :

$this->uploader = $uploader;
$this->adapterFactory = $adapterFactory;
$this->filesystem = $filesystem;
3
1

Use below code:

<?php
/**
 *
 * Copyright © 2015 Contactcommerce. All rights reserved.
 */
namespace Contact\Modules\Controller\Contact;

class Index extends \Magento\Framework\App\Action\Action
{

    /**
     * @var \Magento\Framework\App\Cache\TypeListInterface
     */
    protected $_cacheTypeList;

    /**
     * @var \Magento\Framework\App\Cache\StateInterface
     */
    protected $_cacheState;

    /**
     * @var \Magento\Framework\App\Cache\Frontend\Pool
     */
    protected $_cacheFrontendPool;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;
    protected $_objectManager;
    protected $adapterFactory;
    protected $uploader;
    protected $filesystem;

    /**
     * @param Action\Context $context
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\Framework\App\Cache\StateInterface $cacheState
     * @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\ObjectManagerInterface $objectManager,
            \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
            \Magento\Framework\App\Cache\StateInterface $cacheState,
            \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory,
            \Magento\Framework\Image\AdapterFactory $adapterFactory,
            \Magento\MediaStorage\Model\File\UploaderFactory $uploader,
            \Magento\Framework\Filesystem $filesystem,

            array $data = []
    ) {

        parent::__construct($context, $data);
        $this->_objectManager = $objectManager;
         $this->_cacheTypeList = $cacheTypeList;
        $this->_cacheState = $cacheState;
        $this->_cacheFrontendPool = $cacheFrontendPool;
        $this->uploader = $uploader;
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     * Flush cache storage
     *
     */
    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
//        $email = $this->getRequest()->getParam('email');
//        $subject = $this->getRequest()->getParam('subject');
//        $message = $this->getRequest()->getParam('message');
//        $photo = $this->getRequest()->getParam('photo');
        if(isset($post))
        {
        $contact = $this->_objectManager->create('Contact\Modules\Model\Contact');
        $contact->setData('email', $post['email']);
        $contact->setData('subject', $post['subject']);
        $contact->setData('message', $post['message']);
        if (isset($_FILES['photo']) && isset($_FILES['photo']['name']) && strlen($_FILES['photo']['name']))
            {
            $base_media_path = 'Contact/Modules/images';
            $uploader = $this->uploader->create(
                ['fileId' => 'photo']
                );
                $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
                $imageAdapter = $this->adapterFactory->create();
                $uploader->addValidateCallback('photo', $imageAdapter, 'validateUploadFile');
                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(true);
                $mediaDirectory = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
                $result = $uploader->save(
                $mediaDirectory->getAbsolutePath(base_media_path));
        }
        $post['photo'] = base_media_path.$result['file'];
        $contact->setData('photo', $post['photo']);
        $contact->save();
        }
        else
        {
        $this->resultPage = $this->resultPageFactory->create();  
    return $this->resultPage;
        }
    }
}

You miss the define variable in construct so you got that error.

2
  • This answer is identical to other answer. kindly don't post the identical answer. Oct 4, 2016 at 7:55
  • @QaisarSatti I have not seen answer, while I am writing answer at that time other answer was posted.In both answer have slight time difference Oct 4, 2016 at 8:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.