title: Symfony FatalThrowableError : Call to a member function getId() on null
body: <p>I am doing an e-commerce site, and in the end I realized a bug when modifying the informations of the customers informations:</p>
  
  <p>the error is as follows:</p>
  
  <pre><code>Uncaught PHP Exception Error: "Call to a member function getId() on null" 
  at C:\wamp64\www\caviar-pro\src\Controller\CustomerController.php line 339
  </code></pre>
  
  <p>Here is the part of the code that is problematic:
  <a href="https://i.stack.imgur.com/aBoFp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aBoFp.png" alt="enter image description here"></a></p>
  
  <p>The complete code is the following:</p>
  
  <pre><code>public function updateAction(Request $request): Response
  {
      $configuration = $this-&gt;requestConfigurationFactory-&gt;create($this-&gt;metadata, $request);
      $this-&gt;isGrantedOr403($configuration, ResourceActions::UPDATE);
      $resource = $this-&gt;findOr404($configuration);
      $form = $this-&gt;resourceFormFactory-&gt;create($configuration, $resource);
      $oldInfo = clone $resource;
      $oldAddressInfo = $oldInfo-&gt;getDefaultAddress() ? clone $oldInfo-&gt;getDefaultAddress() : null;
  
      if (in_array($request-&gt;getMethod(), ['POST', 'PUT', 'PATCH'], true) &amp;&amp; $form-&gt;handleRequest($request)-&gt;isValid()) {
          /** @var CustomerInterface $resource */
          $resource = $form-&gt;getData();
          $defaultAddress = $resource-&gt;getDefaultAddress();
          if (!$defaultAddress-&gt;getId()) {
              $defaultAddress-&gt;setFirstName($resource-&gt;getFirstName());
              $defaultAddress-&gt;setLastName($resource-&gt;getLastName());
          }
  
          /** @var ResourceControllerEvent $event */
          $event = $this-&gt;eventDispatcher-&gt;dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource);
          if ($event-&gt;isStopped() &amp;&amp; !$configuration-&gt;isHtmlRequest()) {
              throw new HttpException($event-&gt;getErrorCode(), $event-&gt;getMessage());
          }
          if ($event-&gt;isStopped()) {
              $this-&gt;flashHelper-&gt;addFlashFromEvent($configuration, $event);
              if ($event-&gt;hasResponse()) {
                  return $event-&gt;getResponse();
              }
              return $this-&gt;redirectHandler-&gt;redirectToResource($configuration, $resource);
          }
          try {
              $this-&gt;resourceUpdateHandler-&gt;handle($resource, $configuration, $this-&gt;manager);
              $valueChangedPersonnalInfo = $this-&gt;compareCustomer($oldInfo, $resource);
              $valueChangedAddress = $oldAddressInfo ? $this-&gt;compareAddressCustomer($oldAddressInfo, $resource-&gt;getDefaultAddress()) : [];
              $valueChanged = array_merge($valueChangedPersonnalInfo, $valueChangedAddress);
  
              /*sending email according to template bo*/
              /** @var Email $email */
              $email = $this-&gt;manager-&gt;getRepository('App\Entity\Email\Email')-&gt;findOneByCode(MailerManager::ACCOUNT_MODIFICATION_MAILER_CODE);
              $user = $this-&gt;getUser();
              $emailManager = $this-&gt;emailManager-&gt;letterParagraphBy($email-&gt;getContent(), $email, $user-&gt;getCustomer());
              $emailToSend = $this-&gt;renderView('@SyliusShop/Email/userInfoChange.html.twig', [
                'infoChanged' =&gt; $valueChanged,
                'email' =&gt; $email,
                'emailManager' =&gt; $emailManager,
              ]);
  
              $emailFrom = [$email-&gt;getFromEmail() =&gt; $email-&gt;getFromName()];
              $this-&gt;mailerManager-&gt;sendMail($resource-&gt;getEmail(), $email-&gt;getSubject(), $emailToSend, $emailFrom);
  
          } catch (UpdateHandlingException $exception) {
              if (!$configuration-&gt;isHtmlRequest()) {
                  return $this-&gt;viewHandler-&gt;handle(
                      $configuration,
                      View::create($form, $exception-&gt;getApiResponseCode())
                  );
              }
              $this-&gt;flashHelper-&gt;addErrorFlash($configuration, $exception-&gt;getFlash());
              return $this-&gt;redirectHandler-&gt;redirectToReferer($configuration);
          }
          $postEvent = $this-&gt;eventDispatcher-&gt;dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource);
          if (!$configuration-&gt;isHtmlRequest()) {
              $view = $configuration-&gt;getParameters()-&gt;get('return_content', false) ? View::create($resource, Response::HTTP_OK) : View::create(null, Response::HTTP_NO_CONTENT);
              return $this-&gt;viewHandler-&gt;handle($configuration, $view);
          }
          $this-&gt;flashHelper-&gt;addSuccessFlash($configuration, ResourceActions::UPDATE, $resource);
          if ($postEvent-&gt;hasResponse()) {
              return $postEvent-&gt;getResponse();
          }
          return $this-&gt;redirectHandler-&gt;redirectToResource($configuration, $resource);
      }
      if (!$configuration-&gt;isHtmlRequest()) {
          return $this-&gt;viewHandler-&gt;handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
      }
      $initializeEvent = $this-&gt;eventDispatcher-&gt;dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource);
      if ($initializeEvent-&gt;hasResponse()) {
          return $initializeEvent-&gt;getResponse();
      }
      $carrousels =  $this-&gt;manager-&gt;getRepository(Carrousel::class)-&gt;findOneBy(['code' =&gt; 'my_account_carrousel', 'enabled' =&gt; true]);
  
      $view = View::create()
          -&gt;setData([
              'configuration' =&gt; $configuration,
              'metadata' =&gt; $this-&gt;metadata,
              'resource' =&gt; $resource,
              $this-&gt;metadata-&gt;getName() =&gt; $resource,
              'form' =&gt; $form-&gt;createView(),
              'carrousels' =&gt; $carrousels,
          ])
          -&gt;setTemplate($configuration-&gt;getTemplate(ResourceActions::UPDATE . '.html'))
      ;
      return $this-&gt;viewHandler-&gt;handle($configuration, $view);
  }
  </code></pre>
  
  <p>If someone could tell me the solution to my problem I'm interested because I'm stuck on this part of my site for a while.</p>