Magento – Magento 2: Fatal error: Uncaught Error: Call to a member function getId()

admincustommagento2module

After adding custom module on Server. It's giving below error

 Fatal error: Uncaught Error: Call to a member function getId() on null
 in
 /var/www/html/magento/vendor/magento/module-backend/Block/Menu.php:296
 Stack trace: #0
 /var/www/html/magento/vendor/magento/framework/View/Element/AbstractBlock.php(975):
 Magento\Backend\Block\Menu->getCacheKeyInfo() #1
 /var/www/html/magento/vendor/magento/framework/View/Element/AbstractBlock.php(1027):
 Magento\Framework\View\Element\AbstractBlock->getCacheKey() #2
 /var/www/html/magento/vendor/magento/framework/View/Element/AbstractBlock.php(652):
 Magento\Framework\View\Element\AbstractBlock->_loadCache() #3
 /var/www/html/magento/vendor/magento/framework/View/Layout.php(542):
 Magento\Framework\View\Element\AbstractBlock->toHtml() #4
 /var/www/html/magento/vendor/magento/framework/View/Layout.php(518):
 Magento\Framework\View\Layout->_renderBlock('menu') #5
 /var/www/html/magento/var/generation/Magento/Framework/View/Layout/Interceptor.php(206):
 Magento\Framework\View\Layout->renderNonCachedElement('menu') #6
 /var/www/html/magento/vendor/magento/framewo in
 /var/www/html/magento/vendor/magento/module-backend/Block/Menu.php on
 line 296

It's displaying when Session Timeout from Admin

Best Answer

I was able to get around this issue by linking to an "interstitial" URL first (a controller) which simply redirected to the view URL.

The controller will kick the user back to the login page if the session is expired. If it's not expired, it will redirect to the view.

Hope this helps.