cancel
Showing results for 
Search instead for 
Did you mean: 

Type Error occurred when creating object

Type Error occurred when creating object

Hi.

 

We've made a controller where we are trying to use the curl client. However when we run it on a controller we get an error message. It works fine when we try to run it on our eventobserver however. What could the issue be? So far from searching about it it seems to be something wrong with the constructor or something before that, and not the curl client call itself.

 

We are on version 2.3, should be the newest version.

 

Code for the Controller:

 

<?php

namespace Devchannel\HelloWorld\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\ObjectManager\ObjectManager;
#use Magento\Framework\Controller\ResultFactory;

class Index extends Action {


   protected $curlClient;

   public function __construct(
       \Magento\Framework\App\Action\Context $context,
       \Magento\Framework\HTTP\Client\Curl $curl
   ) {
       $this->curlClient = $curl;
       parent::__construct($context);
   }
   public function execute()
   {
       echo "hello";
       #$serviceUrl = 'https://jsonplaceholder.typicode.com/todos/1';
       #$this->curlClient->get($serviceUrl, []);
       #$this->curl->addHeader("Content-Type", "application/json");
       #$this->curl->addHeader("Accept", "application/json");
       #$this->curl->addHeader("Authorization", "Basic");
        //response will contain the output in form of JSON string
        #$response = $this->curlClient->getBody();

        #echo $response;
   }

}

 

Code for the EventObservers:

<?php

namespace DigitalRevisor\Tripletex\Observer;

use Magento\Framework\ObjectManager\ObjectManager;

class Tripletex implements \Magento\Framework\Event\ObserverInterface {

   /** @var \Magento\Framework\Logger\Monolog */
   protected $_logger;

   /**
    * @var \Magento\Framework\ObjectManager\ObjectManager
    */
   protected $_objectManager;

   protected $_orderFactory;
   protected $_checkoutSession;

   protected $curlClient;

   public function __construct(
       \Psr\Log\LoggerInterface $loggerInterface,
       \Magento\Checkout\Model\Session $checkoutSession,
       \Magento\Sales\Model\OrderFactory $orderFactory,
       \Magento\Framework\ObjectManager\ObjectManager $objectManager,
       \Magento\Framework\HTTP\Client\Curl $curl
   ) {
       $this->_logger = $loggerInterface;
       $this->_objectManager = $objectManager;
       $this->_orderFactory = $orderFactory;
       $this->_checkoutSession = $checkoutSession;
       $this->curlClient = $curl;
   }

   /**
    * This is the method that fires when the event runs.
    *
    * @param \Magento\Framework\Event\Observer $observer
    */
   public function execute(\Magento\Framework\Event\Observer $observer ) {

       $orderIds = $observer->getEvent()->getOrderIds();
        $serviceUrl = 'https://jsonplaceholder.typicode.com/todos/1';
       $this->curlClient->get($serviceUrl, []);
       #$this->curl->addHeader("Content-Type", "application/json");
       #$this->curl->addHeader("Accept", "application/json");
       #$this->curl->addHeader("Authorization", "Basic");
        //response will contain the output in form of JSON string
        $response = $this->curlClient->getBody();

 

Error Message:

1 exception(s):
Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Devchannel\HelloWorld\Controller\Index\Index\Interceptor

Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Devchannel\HelloWorld\Controller\Index\Index\Interceptor
<pre>#1 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Devchannel\Hello...', array()) called at [vendor/magento/framework/ObjectManager/ObjectManager.php:56]
#2 Magento\Framework\ObjectManager\ObjectManager->create('Devchannel\Hello...') called at [vendor/magento/framework/App/ActionFactory.php:44]
#3 Magento\Framework\App\ActionFactory->create('Devchannel\Hello...') called at [vendor/magento/framework/App/Router/Base.php:306]
#4 Magento\Framework\App\Router\Base->matchAction(&Magento\Framework\App\Request\Http#000000001b29d8820000000016eb34a9#, array('moduleFrontName' => 'devchannel', 'actionPath' => 'index', 'actionName' => 'index')) called at [vendor/magento/framework/App/Router/Base.php:167]
#5 Magento\Framework\App\Router\Base->match(&Magento\Framework\App\Request\Http#000000001b29d8820000000016eb34a9#) called at [vendor/magento/framework/App/FrontController.php:95]
#6 Magento\Framework\App\FrontController->dispatch(&Magento\Framework\App\Request\Http#000000001b29d8820000000016eb34a9#) called at [vendor/magento/framework/Interception/Interceptor.php:58]
#7 Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', array(&Magento\Framework\App\Request\Http#000000001b29d8820000000016eb34a9#)) called at [vendor/magento/framework/Interception/Interceptor.php:138]
#8 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#000000001b29d8820000000016eb34a9#) called at [vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php:94]
#9 Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(&Magento\Framework\App\FrontController\Interceptor#000000001b29d9720000000016eb34a9#, &Closure#000000001b29d95b0000000016eb34a9#, &Magento\Framework\App\Request\Http#000000001b29d8820000000016eb34a9#) called at [vendor/magento/framework/Interception/Interceptor.php:135]
#10 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#000000001b29d8820000000016eb34a9#) called at [vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php:73]
#11 Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(&Magento\Framework\App\FrontController\Interceptor#000000001b29d9720000000016eb34a9#, &Closure#000000001b29d95b0000000016eb34a9#, &Magento\Framework\App\Request\Http#000000001b29d8820000000016eb34a9#) called at [vendor/magento/framework/Interception/Interceptor.php:135]
#12 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#000000001b29d8820000000016eb34a9#) called at [vendor/magento/framework/Interception/Interceptor.php:153]
#13 Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', array(&Magento\Framework\App\Request\Http#000000001b29d8820000000016eb34a9#), NULL) called at [generated/code/Magento/Framework/App/FrontController/Interceptor.php:26]
#14 Magento\Framework\App\FrontController\Interceptor->dispatch(&Magento\Framework\App\Request\Http#000000001b29d8820000000016eb34a9#) called at [vendor/magento/framework/App/Http.php:137]
#15 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:24]
#16 Magento\Framework\App\Http\Interceptor->launch() called at [vendor/magento/framework/App/Bootstrap.php:261]
#17 Magento\Framework\App\Bootstrap->run(&Magento\Framework\App\Http\Interceptor#000000001b29d8fc0000000016eb34a9#) called at [index.php:39]
</pre>

 

5 REPLIES 5

Re: Type Error occurred when creating object

The interceptor file needs to be re-create. So kindly run di:compile and clean cache

 

php bin/magento setup:di:compile
php  bin/magento  cache:clean

 

Re: Type Error occurred when creating object

Just Remove the Generation folder or in version 2.2 remove the generated folder.

rm -rf generated/

Flush the cache. It should work then. Its because the interceptor file needs to be re-created or refresh.

Re: Type Error occurred when creating object

Hello, could you help me? the 'rm' in this 'rm -rf generated/'  is don't recognize in me cmd

Re: Type Error occurred when creating object

In CMD, use cd to your magento root  folder and try the below command

rm -rf generated/

Re: Type Error occurred when creating object

Anyone still facing the same issue with magento 2.3.4?

php bin/magento cache:flush

An exception was raised while creating "Request"; no instance returned

The reason was redis cache. Flush it and everything will work smooth.

redis-cli -h HOSTNAME

flushall