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..

Raphael at Digital Pianism
70.8k37 gold badges192 silver badges357 bronze badges
asked Oct 4, 2016 at 7:07

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;
answered Oct 4, 2016 at 7:10
3
  • I have already declared. By mistake it is not showing on question. Commented Oct 4, 2016 at 7:14
  • sorry sorry i actually forgot ;) Commented Oct 4, 2016 at 7:15
  • @Ramkishan not a problem ;) Commented Oct 4, 2016 at 7:17
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.

answered Oct 4, 2016 at 7:13
2
  • This answer is identical to other answer. kindly don't post the identical answer. Commented 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 Commented Oct 4, 2016 at 8:58

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.