askcoder
askcoder

Reputation: 913

Magento call to a member function get() on null

I am working on Magento 2.4 on customizing the error provided by error 404 , if anyone has any idea about this :

Error: Call to a member function get() on null in C:\xampp-clean\htdocs\magento242\vendor\magento\framework\App\Action\Action.php:109 Stack trace: #0 C:\xampp-clean\htdocs\magento242\vendor\magento\framework\App\FrontController.php(186): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http)) #1 C:\xampp-clean\htdocs\magento242\vendor\magento\framework\App\FrontController.php(118): Magento\Framework\App\FrontController->processRequest(Object(Magento\Framework\App\Request\Http), Object(SimplifiedMagento\RequestFlow\Controller\Page\CustomNoRoute\Interceptor))

This is the function (Controller)

<?php
namespace SimplifiedMagento\RequestFlow\Controller\Page;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\ResponseInterface;

class CustomNoRoute extends Action
{

    public function execute()
    {
        echo "this is our custom 404";
    }
}

After changing the code to this

<?php
namespace SimplifiedMagento\RequestFlow\Controller\Page;

use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\ResponseInterface;

class CustomNoRoute implements ActionInterface
{

    public function execute()
    {
        echo "this is our custom 404";
    }
}

I am getting this result :

this is our custom 4041 exception(s):
Exception #0 (InvalidArgumentException): Invalid return type

Exception #0 (InvalidArgumentException): Invalid return type
<pre>#1 Magento\Framework\App\Http\Interceptor->launch() called at [vendor\magento\framework\App\Bootstrap.php:263]
#2 Magento\Framework\App\Bootstrap->run() called at [pub\index.php:29]
</pre>

So at least it is echoing the result ; but still showing exception error.

If anyone has an idea? thanks in advance

Upvotes: 0

Views: 7880

Answers (2)

Axi
Axi

Reputation: 1805

As stated later in the question by the OP, implementing
Magento\Framework\App\ActionInterface
instead of extending
Magento\Framework\App\Action\Action

fixes

Error: Call to a member function get() on null in vendor\magento\framework\App\Action\Action.php:109

Upvotes: 0

Mallika Jaiswal
Mallika Jaiswal

Reputation: 1

change your code to this it will echo the result without any exception.

<?php

namespace SimplifiedMagento\RequestFlow\Controller\Page;

class CustomNoRoute extends \Magento\Backend\App\Action
{

    public function execute()
    {
        echo "this is our custom 404";
    }
}

Run the following commands -:

  1. rm -rf var/ generated/
  2. php bin/magento s:d:c
  3. php bin/magento s:s:d -f
  4. chmod -R 777 var/ generated/

Upvotes: -1

Related Questions