Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Call to a member function getId() on null in doctrine

I have a error saying that,

Fatal error: Call to a member function getId() on null.

but how can i check the element, here is my code

$infoArray['groupId'] = $info->getId();
$infoArray['name'] = $info->getName();
$infoArray['addressLine1'] = $info->getAddressLine1();
$infoArray['addressLine2'] = $info->getAddressLine2();
$infoArray['isActive'] = $info->getActive();
$infoArray['countryId'] = $info->getCountry()->getId(); //Here is my error line
$infoArray['countryName'] = $info->getCountry()->getName();
$infoArray['stateId'] = $info->getState()->getId();
$infoArray['stateName'] = $info->getState()->getName();
$infoArray['cityId'] = $info->getCity()->getId();
$infoArray['cityName'] = $info->getCity()->getName();
$infoArray['areaId'] = $info->getArea()->getId();
$infoArray['areaName'] = $info->getArea()->getName();
$infoArray['zipcode'] = $info->getZipcode();
like image 259
user3929758 Avatar asked Jan 28 '26 02:01

user3929758


1 Answers

Replace the line by :

$infoArray['countryId'] = $info->getCountry() ? $info->getCountry()->getId() : null;

Do the same for each line corresponding to an association (which can be null), and null will be returned if the object is null.

like image 176
chalasr Avatar answered Jan 30 '26 15:01

chalasr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!