cancel
Showing results for 
Search instead for 
Did you mean: 

Type Error occurred when creating object:

Type Error occurred when creating object:

I am having the massaction "delete" in the ui_component and it is then process by a controller MassDelete in my adminhtml folder.

like this:

<?php

namespace Apriljune\Testimonial\Controller\Adminhtml\Testimonial;

use Exception;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Apriljune\Testimonial\Model\ResourceModel\Testimonial\Collection as Testimonial;

/**
* Class MassDelete
*
* @package Apriljune\Testimonial\Controller\Adminhtml\Testimonial
*/
class MassDelete extends Action
{

   /**
    * @var Testimonial
    */
   protected $testimonial;


   /**
    * @param Context $context
    * @param Testimonial $testimonial
    */
   public function __construct( Context $context, Testimonial $testimonial )
   {
       parent::__construct($context);
       $this->testimonial = $testimonial;
   }

   /**
    * Execute action
    *
    * @return \Magento\Backend\Model\View\Result\Redirect
    */
   public function execute()
   {
       $selectedIds = $this->getRequest()->getParams()['selected'];
       if (!is_array($selectedIds)) {
           $this->messageManager->addErrorMessage(__('Please select one or more testimonial.'));
       } else {
           try {
               $collectionSize = count($selectedIds);
               foreach ($selectedIds as $_id) {
                   $testimonial = $this->testimonial->getItems()[$_id];
                   $testimonial->delete();
               }
               $this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been deleted.', $collectionSize));
           } catch (Exception $e) {
               $this->messageManager->addErrorMessage($e->getMessage());
           }
       }

       /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
       $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
       return $resultRedirect->setPath('*/*/');
   }
}

and massaction is like this in ui-component

<massaction name="listing_massaction">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
            </item>
        </argument>
        <action name="delete">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">delete</item>
                    <item name="label" xsi:type="string" translate="true">Delete</item>
                    <item name="url" xsi:type="url" path="*/*/MassDelete"/>
                    <item name="confirm" xsi:type="array">
                      <item name="title" xsi:type="string" translate="true">Delete</item>
                      <item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item>
                  </item>
              </item>
          </argument>
      </action>
  </massaction>

Now when i select any row and delete it it redirect to the error page and show the exception

Type Error occurred when creating object: Apriljune\Testimonial\Controller\Adminhtml\Testimonial\MassDelete\Interceptor

i have already tried the solutions,

already done with this process as well

rm -rf var/di/* pub/static/* generated/*
rm -rf var/cache/* var/view_preprocessed/* 

bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento setup:static-content:deploy
bin/magento cache:flush

Interceptor is already create in /generated i have checked it.

But still the error is there any hint why it is showing error? any typo or logical error? BTW i am using 2.3.3 version of Magento

15 REPLIES 15

Re: Type Error occurred when creating object:

Hi @Shoaib967 


I have checked, you backend controller code is correct. This issue belongs to compile issue.
Please set permission then retry:

rm -rf generated/*
chmod -R 777 var/ pub/ generated/
php bin/magento setup:di:compile
php bin/magento cache:flush
chmod -R 777 var/ pub/ generated/

It may help!
Problem Sovled? Please click on  'Kudos' & Accept as Solution!

Problem solved? Click Accept as Solution!

Re: Type Error occurred when creating object:

Hi @Bhanu Periwal 

Thanks for your time!

It is still the same error not resolved this is the error screen:

 

https://prnt.sc/tjieik

 

Re: Type Error occurred when creating object:

Hi @Shoaib967 ,

 

I have created massDelete controller in one of the module and the content of the controller is like below and it is working fine for me.

public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Ui\Component\MassAction\Filter $filter,
        CollectionFactory $collectionFactory
    ) {
        $this->filter = $filter;
        $this->collectionFactory = $collectionFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $collection = $this->filter->getCollection($this->collectionFactory->create());
        $compDeleted = 0;
        foreach ($collection->getItems() as $comp) {
            $comp->delete();
            ++$compDeleted;
        }
        $this->messageManager->addSuccess(
            __('A total of %1 record(s) have been deleted.', $compDeleted)
        );

        $resultFactory = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

        return $resultFactory->setPath('vtn_hlparts/components/index');
    }

Try to change your code is like above code.

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

Re: Type Error occurred when creating object:

Please try below commands :

 

sudo chmod -R 777 var/ generated/ pub/static/
php bin/magento setup:di:compile
php bin/magento c:f
sudo chmod -R 777 var/ generated/ pub/static/

Hope it Helps !

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

Re: Type Error occurred when creating object:

Hi @Nishu Jindal ,

 

Thanks for looking into the issue,

 

i have tried the code in this way, but still getting the same error. i have also checked the permissions and set to 777 for generated, but still not luck

<?php

namespace Apriljune\Testimonial\Controller\Adminhtml\Testimonial;

use Exception;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Apriljune\Testimonial\Model\ResourceModel\Testimonial\Collection as Testimonial;

/**
 * Class MassDelete
 *
 * @package Apriljune\Testimonial\Controller\Adminhtml\Testimonial
 */
class MassDelete extends Action
{

    /**
     * @var Testimonial
     */
    protected $testimonial;

    /**
     * Message manager interface
     *
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;

    /**
     * @var \Magento\Framework\Controller\ResultFactory
     */
    protected $resultFactory;


    /**
     * MassDelete constructor.
     * @param Context $context
     * @param Testimonial $testimonial
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Ui\Component\MassAction\Filter $filter,
        Testimonial $testimonial
    ) {
        $this->filter = $filter;
        $this->testimonial = $testimonial;
        parent::__construct($context);
    }

    public function execute()
    {
        $collection = $this->filter->getCollection($this->testimonial->create());
        $compDeleted = 0;
        foreach ($collection->getItems() as $comp) {
            $comp->delete();
            ++$compDeleted;
        }
        $this->messageManager->addSuccess(
            __('A total of %1 record(s) have been deleted.', $compDeleted)
        );

        $resultFactory = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

        return $resultFactory->setPath('vtn_hlparts/components/index');
    }
}

Re: Type Error occurred when creating object:

Hi @Shoaib967,

 

I hope you have cleared the generated folder and run the setup:di:compile command after the controller changes.

 

Thanks!

Re: Type Error occurred when creating object:

Yes i have run the command, and still having the issue after,

Re: Type Error occurred when creating object:

Hi @Shoaib967,

Can you perform below changes

1. In  controller constructor add collection class directly and with factory (same way I have added in my code)

2. Can you share your module structure with us (screenhsot)

3. Last can you simply delete everything from execute and simply write echo and exit command to check whether controller is working fine or not.

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

Re: Type Error occurred when creating object:

Here is the module structure by the way i will try rest of the points.

https://prnt.sc/tjumdm

more over a full code overview i have posted in other question here, you can have a look there as well
https://magento.stackexchange.com/questions/317489/admin-grid-not-showing-data-with-ui-component