0

I have some problem about this error, who can tell me why? Thank you!! Fatal error: Uncaught Error: Call to a member function getId() on null in

closed as unclear what you're asking by Manashvi Birla, Jai, saravanavelu, Mohit Kumar Arora, HelgeB Jun 16 at 8:31

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please detail $customer = $this->helperData->getCustomer(); – HoangHieu Jun 10 at 8:14
  • sorry,what your mean? @HoangHieu – niceonelee Jun 10 at 8:23
  • $this->helperData->getCustomer(); this function returned null, please detail it. – HoangHieu Jun 10 at 8:25
  • This have no return, just return $collection. and when i changed other code it can work for no login, but login will be error. @HoangHieu – niceonelee Jun 10 at 8:33
  • did you print_r($customer) to check is it returning something or not? – Asad Khan Jun 10 at 8:39
1

Your error is more logic than syntax, if the customer isn't logged in (guest) you cannot get the user id. I would edit your helper function to check if the user is logged in and return false if not.

public function getCustomer()
{
    $customerSession = $this->customerSession->create();
    $customerData = $customerSession->getCustomerData();
    if($customerSession->isLoggedIn()){
        return $customerData;
     } else{
         return false;
     }
}

Then in your getTicket function use something like this :

if ($this->helperData->getCustomer()){
  // run your code as normal
} else {
  // redirect to login
}
  • you should mark this as the answer in that case – bjornredemption Jun 10 at 13:39
  • yes,marked,can you help me see the second issue,thank you @bjornredemption – niceonelee Jun 10 at 13:44
0
 $user_id = $customer->getId(); 

this code return int or null,when return null,the program will automatically throw an error.You should handle your mistakes properly.eg:

if ($user_id) {
//your code
}else{
 //your code
}
  • im still getting error, can you made a full code let me to see what i wrong, thank you!! @tokey – niceonelee Jun 10 at 10:03
  • getting this 1 exception(s): Exception #0 (Exception): Notice: Undefined variable: user_id – niceonelee Jun 10 at 10:06
  • @niceonelee Please show your complete code. – tokey Jun 10 at 10:09
  • please check the original post, this is full file code, thank you! @tokey – niceonelee Jun 10 at 10:16
  • @niceonelee But you didn't modify your code as I suggested. – tokey Jun 10 at 10:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.