Nullable value in command file

  doctrine, getter, null, repository, symfony

I’m actually working on a symfony project where i have to get into the DB all the taxis with some informations about them and make them write in a CSV file.

For this, i’ve create a CiblageCommand File :


namespace AppBundleCommand;

use AppBundleEntityTaxi;
use AppBundleEntityStatutTaxi;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use DoctrineORMEntityManagerInterface;
use SymfonyComponentConsoleStyleSymfonyStyle;
use SymfonyBundleFrameworkBundleCommandContainerAwareCommand;

class CiblageCommand extends ContainerAwareCommand
private $em;

public function __construct(EntityManagerInterface $em)
    $this->em = $em;

protected function configure()
        //the short description shown while running "php bin/console list"
        ->setDescription('cible les taxis')
        ->setname('ciblage')//php bin/console ciblage
        //the full command descriptionn shown when running the command with
        //the "--help" option
        ->setHelp('Cette commande permet de cibler les taxis valides présents'
            .' dans la BDD et de leur générer un PDF conforme.')

protected function execute(InputInterface $input, OutputInterface $output)
    $io = new SymfonyStyle($input, $output);
    $io->title("Lancement de la génération");

    $io->title("nListage des taxis");
    $io->title("nCiblage terminé");

    $io->title("nGénération des courriers");
    $io->success("nGénération terminée");

protected function create()
    $repository_taxi = $this->em->getRepository('AppBundle:Taxi');
    $listTaxi = $repository_taxi->findAll();

    $doctrine = $this->getContainer()->get('doctrine');
    $em = $doctrine->getEntityManager();

    $handle = fopen('CSVTaxi', 'w+');
    fputcsv($handle, ['IDENTIFIANT', 'RAISON SOCIALE', 'STATUT', 'ETAT'], ';');

    foreach ($listTaxi as $taxi)
        if($taxi == null){
            $listTaxi[$taxi] = "NULL" ;
    echo("nbr taxi : ".count($listTaxi)."n");

However, when i tried to do a “getStatutTaxi()” the value return is null and i cant write on the file despite the if before.

there is the error my terminal gave me

[[email protected] 10:23:23] [~/web/html/scot] $ php bin/console ciblage

Lancement de la génération

Listage des taxis

10:17:16 ERROR     [console] Error thrown while running command "ciblage". Message: "Call to a 
member function getLibelleStatut() on null" ["exception" => Error { …},"command" => 
"ciblage","message" => "Call to a member function getLibelleStatut() on null"]
PHP Fatal error:  Uncaught SymfonyComponentDebugExceptionFatalThrowableError: Call to a member 
function getLibelleStatut() on null in 
Stack trace:
#0 /app/apl4e04/web/html/scot/src/AppBundle/Command/CiblageCommand.php(44): 
#2 /app/apl4e04/web/html/scot/vendor/symfony/symfony/src/Symfony/
Component/Console/Application.php(987): SymfonyComponentConsoleCommandCommand- 
 .php(86): SymfonyComponentConsoleApplication->doRunCommand(Object(AppBundl in 
/app/apl4e04/web/html/scot/src/AppBundle/Command/CiblageCommand.php on line 74

How can i fix this and make this work ?

Thanks in advance for your help.

Source: Symfony Questions