user3929758
user3929758

Reputation: 233

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();

Upvotes: 3

Views: 21907

Answers (2)

chalasr
chalasr

Reputation: 13167

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.

Upvotes: 5

user1756634
user1756634

Reputation: 128

check with is_null($object_name)

Upvotes: 2

Related Questions