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..
Best Answer
You forgot to declare some protected variables, you need to add the following
And also add the following to your
__construct()
: