guest Impossible to access an attribute ("username") on a null variable. (500 Internal Server Error)

Symfony Exception

Twig_Error_Runtime

HTTP 500 Internal Server Error

Impossible to access an attribute ("username") on a null variable.

Exception

Twig_Error_Runtime

  1. <h1>Hello World!</h1>
  2. {{ user.username }}
twig_get_attribute(object(Twig_Environment), object(Twig_Source), null, 'username', array()) in var/cache/dev/twig/93/93424119945f1aa72d47dd3a00b060cee7e4a1556fd3bbf608d99a1b7863da7e.php (line 30)
  1.         // line 1
  2.         echo "<h1>Hello World!</h1>
  3. ";
  4.         // line 3
  5.         echo twig_escape_filter($this->envtwig_get_attribute($this->env$this->source, (isset($context["user"]) || array_key_exists("user"$context) ? $context["user"] : (function () { throw new Twig_Error_Runtime('Variable "user" does not exist.'3$this->source); })()), "username", array()), "html"nulltrue);
  6.         
  7.         $__internal_319393461309892924ff6e74d6d6e64287df64b63545b994e100d4ab223aed02->leave($__internal_319393461309892924ff6e74d6d6e64287df64b63545b994e100d4ab223aed02_prof);
  8.     }
__TwigTemplate_4aad86580d9dfbaf814d2d0fd0cf8499ec19a50826abdac06fadb0c0964c0cb6->doDisplay(array('user' => null, 'app' => object(AppVariable)), array()) in vendor/twig/twig/lib/Twig/Template.php (line 390)
  1.     }
  2.     protected function displayWithErrorHandling(array $context, array $blocks = array())
  3.     {
  4.         try {
  5.             $this->doDisplay($context$blocks);
  6.         } catch (Twig_Error $e) {
  7.             if (!$e->getSourceContext()) {
  8.                 $e->setSourceContext($this->getSourceContext());
  9.             }
Twig_Template->displayWithErrorHandling(array('user' => null, 'app' => object(AppVariable)), array()) in vendor/twig/twig/lib/Twig/Template.php (line 367)
  1.         return $this->blocks;
  2.     }
  3.     public function display(array $context, array $blocks = array())
  4.     {
  5.         $this->displayWithErrorHandling($this->env->mergeGlobals($context), array_merge($this->blocks$blocks));
  6.     }
  7.     public function render(array $context)
  8.     {
  9.         $level ob_get_level();
Twig_Template->display(array('user' => null)) in vendor/twig/twig/lib/Twig/Template.php (line 375)
  1.     public function render(array $context)
  2.     {
  3.         $level ob_get_level();
  4.         ob_start();
  5.         try {
  6.             $this->display($context);
  7.         } catch (Throwable $e) {
  8.             while (ob_get_level() > $level) {
  9.                 ob_end_clean();
  10.             }
Twig_Template->render(array('user' => null)) in vendor/twig/twig/lib/Twig/Environment.php (line 289)
  1.      * @throws Twig_Error_Syntax  When an error occurred during compilation
  2.      * @throws Twig_Error_Runtime When an error occurred during rendering
  3.      */
  4.     public function render($name, array $context = array())
  5.     {
  6.         return $this->loadTemplate($name)->render($context);
  7.     }
  8.     /**
  9.      * Displays a template.
  10.      *
Twig_Environment->render('hello.html.twig', array('user' => null)) in vendor/symfony/framework-bundle/Controller/ControllerTrait.php (line 286)
  1.     protected function render($view, array $parameters = array(), Response $response null)
  2.     {
  3.         if ($this->container->has('templating')) {
  4.             $content $this->container->get('templating')->render($view$parameters);
  5.         } elseif ($this->container->has('twig')) {
  6.             $content $this->container->get('twig')->render($view$parameters);
  7.         } else {
  8.             throw new \LogicException('You can not use the "render" method if the Templating Component or the Twig Bundle are not available. Try running "composer require symfony/twig-bundle".');
  9.         }
  10.         if (null === $response) {
Controller->render('hello.html.twig', array('user' => null)) in src/Controller/SimpleController.php (line 37)
  1.         /*$visitor = \XF::visitor()->toArray();
  2.         $userId = $visitor['user_id'];
  3.         $username = $visitor['username'];
  4.         echo "User id = ".$userId;*/
  5.         $content $request->cookies->all();
  6.         return $this->render('hello.html.twig', ['user' => $user]);
  7.     }
  8. }
  9. /*
SimpleController->index(object(Request)) in vendor/symfony/http-kernel/HttpKernel.php (line 151)
  1.         $this->dispatcher->dispatch(KernelEvents::CONTROLLER_ARGUMENTS$event);
  2.         $controller $event->getController();
  3.         $arguments $event->getArguments();
  4.         // call controller
  5.         $response = \call_user_func_array($controller$arguments);
  6.         // view
  7.         if (!$response instanceof Response) {
  8.             $event = new GetResponseForControllerResultEvent($this$request$type$response);
  9.             $this->dispatcher->dispatch(KernelEvents::VIEW$event);
HttpKernel->handleRaw(object(Request), '1') in vendor/symfony/http-kernel/HttpKernel.php (line 68)
  1.     public function handle(Request $request$type HttpKernelInterface::MASTER_REQUEST$catch true)
  2.     {
  3.         $request->headers->set('X-Php-Ob-Level'ob_get_level());
  4.         try {
  5.             return $this->handleRaw($request$type);
  6.         } catch (\Exception $e) {
  7.             if ($e instanceof RequestExceptionInterface) {
  8.                 $e = new BadRequestHttpException($e->getMessage(), $e);
  9.             }
  10.             if (false === $catch) {
HttpKernel->handle(object(Request), '1', true) in vendor/symfony/http-kernel/Kernel.php (line 200)
  1.         $this->boot();
  2.         ++$this->requestStackSize;
  3.         $this->resetServices true;
  4.         try {
  5.             return $this->getHttpKernel()->handle($request$type$catch);
  6.         } finally {
  7.             --$this->requestStackSize;
  8.         }
  9.     }
Kernel->handle(object(Request)) in public/index.php (line 25)
  1.     Request::setTrustedHosts([$trustedHosts]);
  2. }
  3. $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
  4. $request Request::createFromGlobals();
  5. $response $kernel->handle($request);
  6. $response->send();
  7. $kernel->terminate($request$response);

Stack Trace

Twig_Error_Runtime

Twig_Error_Runtime:
Impossible to access an attribute ("username") on a null variable.

  at templates/hello.html.twig:3
  at twig_get_attribute(object(Twig_Environment), object(Twig_Source), null, 'username', array())
     (var/cache/dev/twig/93/93424119945f1aa72d47dd3a00b060cee7e4a1556fd3bbf608d99a1b7863da7e.php:30)
  at __TwigTemplate_4aad86580d9dfbaf814d2d0fd0cf8499ec19a50826abdac06fadb0c0964c0cb6->doDisplay(array('user' => null, 'app' => object(AppVariable)), array())
     (vendor/twig/twig/lib/Twig/Template.php:390)
  at Twig_Template->displayWithErrorHandling(array('user' => null, 'app' => object(AppVariable)), array())
     (vendor/twig/twig/lib/Twig/Template.php:367)
  at Twig_Template->display(array('user' => null))
     (vendor/twig/twig/lib/Twig/Template.php:375)
  at Twig_Template->render(array('user' => null))
     (vendor/twig/twig/lib/Twig/Environment.php:289)
  at Twig_Environment->render('hello.html.twig', array('user' => null))
     (vendor/symfony/framework-bundle/Controller/ControllerTrait.php:286)
  at Symfony\Bundle\FrameworkBundle\Controller\Controller->render('hello.html.twig', array('user' => null))
     (src/Controller/SimpleController.php:37)
  at App\Controller\SimpleController->index(object(Request))
     (vendor/symfony/http-kernel/HttpKernel.php:151)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
     (vendor/symfony/http-kernel/HttpKernel.php:68)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
     (vendor/symfony/http-kernel/Kernel.php:200)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:25)