0

Getting below error while clicking on edit action from Grid.

Fatal error: Uncaught Error: Call to a member function getUrl() on null in /var/www/html/myproject/machpack/app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Button/Generic.php:60 Stack trace: #0 /var/www/html/myproject/machpack/app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Button/Delete.php(38): Vendor\Module\Block\Adminhtml\Module\Edit\Button\Generic->getUrl('//delete', Array) #1 /var/www/html/myproject/machpack/app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Button/Delete.php(28): Vendor\Module\Block\Adminhtml\Module\Edit\Button\Delete->getDeleteUrl() #2 /var/www/html/myproject/magento236/vendor/magento/framework/View/Element/UiComponent/Context.php(285): Vendor\Module\Block\Adminhtml\Module\Edit\Button\Delete->getButtonData() #3 /var/www/html/myproject/machpack/vendor/magento/module-ui/Component/AbstractComponent.php(116): Magento\Framework\View\Element\UiComponent\Context->addButtons(Array, Object(Magento\Ui\Component\Form)) #4 /var/www/html/myproject/mac in /var/www/html/myproject/machpack/app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Button/Generic.php on line 60

Generic.php

<?php

namespace Vendor\Module\Block\Adminhtml\Module\Edit\Button;

use Magento\Search\Controller\RegistryConstants;
/**
 * Class Generic
 */
class Generic
{
    /**
     * Url Builder
     *
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlBuilder;

    /**
     * Registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $registry;

    /**
     * Constructor
     *
     * @param \Magento\Backend\Block\Widget\Context $context
     * @param \Magento\Framework\Registry $registry
     */
    public function __construct(
        \Magento\Backend\Block\Widget\Context $context,
        \Magento\Framework\Registry $registry
    ) {
        $this->urlBuilder = $context->getUrlBuilder();
        $this->registry = $registry;
    }

    /**
     * Return the synonyms group Id.
     *
     * @return int|null
     */
    public function getId()
    {
        $contact = $this->registry->registry('Module');
        return $contact ? $contact->getId() : null;
    }

    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = [])
    {
        return $this->urlBuilder->getUrl($route, $params);
    }
}
1
1

Try this in your Generic.php file

<?php

namespace Vendor\Module\Block\Adminhtml\ControllerName\Edit;

use Magento\Backend\Block\Widget\Context;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;

/**
 * Class Generic
 */
class Generic
{
    /**
     * @var Context
     */
    protected $context;
     /**
      * @var PageRepositoryInterface
      */
    protected $pageRepository;

    /**
     * @param Context $context
     */
    public function __construct(
        Context $context,
        PageRepositoryInterface $pageRepository
    ) {
        $this->context = $context;
        $this->pageRepository = $pageRepository;
    }

    /**
     * Return CMS block ID
     *
     * @return int|null
     */
   
    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = [])
    {
        return $this->context->getUrlBuilder()->getUrl($route, $params);
    }
   
}
2
  • your code worked correctly. but i still not get where i was making mistake? – Fena shah Jan 5 at 11:28
  • Compare getUrl method of both – Abdul Samad Abbasi Jan 5 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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