Error: Call to a member function hasField() on null

Created on 25 January 2024, 5 months ago
Updated 8 March 2024, 4 months ago

Problem/Motivation

In our logs we are spotting Error: Call to a member function hasField() on null errors, its related to a missing profile.

Error: Call to a member function hasField() on null in Drupal\\social_activity\\EmailTokenServices->getUserPreview() 
(line 252 of /app/html/profiles/contrib/social/modules/social_features/social_activity/src/EmailTokenServices.php) 
#0 /app/html/profiles/contrib/social/modules/social_features/social_group/social_group.tokens.inc(207): 
Drupal\\social_activity\\EmailTokenServices->getUserPreview(Object(Drupal\\social_user\\Entity\\User)) 
#1 /app/html/core/lib/Drupal/Core/Extension/ModuleHandler.php(545): social_group_tokens_alter(Array, Array, 
Object(Drupal\\Core\\Render\\BubbleableMetadata)) #2 /app/html/core/lib/Drupal/Core/Utility/Token.php(368): 
Drupal\\Core\\Extension\\ModuleHandler->alter('tokens', Array, Array, Object(Drupal\\Core\\Render\\BubbleableMetadata)) 
#3 /app/html/core/lib/Drupal/Core/Utility/Token.php(241): Drupal\\Core\\Utility\\Token->generate('message', Array, Array, Array, Object(Drupal\\Core\\Render\\BubbleableMetadata)) #4 /app/html/core/lib/Drupal/Core/Utility/Token.php(191): Drupal\\Core\\Utility\\Token->doReplace(true, '<p><a href=\"[me...', Array, Array, Object(Drupal\\Core\\Render\\BubbleableMetadata)) #5 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/ActivityFactory.php(593): Drupal\\Core\\Utility\\Token->replace('<p><a href=\"[me...', Array, Array, Object(Drupal\\Core\\Render\\BubbleableMetadata)) #6 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/ActivityFactory.php(513): Drupal\\activity_creator\\ActivityFactory->processTokens(Array, Array, Object(Drupal\\message\\Entity\\Message)) #7 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/ActivityFactory.php(204): Drupal\\activity_creator\\ActivityFactory->getMessageText(Object(Drupal\\message\\Entity\\Message)) #8 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/ActivityFactory.php(135): Drupal\\activity_creator\\ActivityFactory->getFieldOutputText(Object(Drupal\\message\\Entity\\Message)) #9 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/ActivityFactory.php(110): Drupal\\activity_creator\\ActivityFactory->buildActivities(Array) #10 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/Plugin/QueueWorker/ActivityWorkerActivities.php(61): Drupal\\activity_creator\\ActivityFactory->createActivities(Array) #11 /app/html/core/lib/Drupal/Core/Cron.php(183): Drupal\\activity_creator\\Plugin\\QueueWorker\\ActivityWorkerActivities->processItem(Array) #12 /app/html/modules/contrib/ultimate_cron/src/UltimateCron.php(70): Drupal\\Core\\Cron->processQueues() #13 /app/html/modules/contrib/ultimate_cron/src/ProxyClass/UltimateCron.php(70): Drupal\\ultimate_cron\\UltimateCron->run() #14 /app/vendor/drush/drush/src/Commands/core/DrupalCommands.php(63): Drupal\\ultimate_cron\\ProxyClass\\UltimateCron->run() #15 [internal function]: Drush\\Commands\\core\\DrupalCommands->cron(Array) #16 /app/vendor/consolidation/annotated-command/src/CommandProcessor.php(276): call_user_func_array(Array, Array) #17 /app/vendor/consolidation/annotated-command/src/CommandProcessor.php(212): Consolidation\\AnnotatedCommand\\CommandProcessor->runCommandCallback(Array, Object(Consolidation\\AnnotatedCommand\\CommandData)) #18 /app/vendor/consolidation/annotated-command/src/CommandProcessor.php(176): 
Consolidation\\AnnotatedCommand\\CommandProcessor->validateRunAndAlter(Array, Array, Object(Consolidation\\AnnotatedCommand\\CommandData)) #19 /app/vendor/consolidation/annotated-command/src/AnnotatedCommand.php(391): Consolidation\\AnnotatedCommand\\CommandProcessor->process(Object(Symfony\\Component\\Console\\Output\\ConsoleOutput), Array, Array, Object(Consolidation\\AnnotatedCommand\\CommandData)) #20 /app/vendor/symfony/console/Command/Command.php(326): Consolidation\\AnnotatedCommand\\AnnotatedCommand->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #21 /app/vendor/symfony/console/Application.php(1096): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #22 /app/vendor/symfony/console/Application.php(324): Symfony\\Component\\Console\\Application->doRunCommand(Object(Consolidation\\AnnotatedCommand\\AnnotatedCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #23 /app/vendor/symfony/console/Application.php(175): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #24 /app/vendor/drush/drush/src/Runtime/Runtime.php(110): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #25 /app/vendor/drush/drush/src/Runtime/Runtime.php(40): Drush\\Runtime\\Runtime->doRun(Array, Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #26 /app/vendor/drush/drush/drush.php(139): Drush\\Runtime\\Runtime->run(Array) #27 /app/vendor/drush/drush/drush(4): require('/app/vendor/dru...') #28 /app/vendor/bin/drush(119): include('/app/vendor/dru...') #29 {main}. {\"exception\":\"[object] (Error(code: 0): Call to a member function hasField() on null at /app/html/profiles/contrib/social/modules/social_features/social_activity/src/EmailTokenServices.php:252)\",\"severity_level\":3}

Steps to reproduce

Create a user without a profile and send them an email.

Proposed resolution

Even though the profile should always be there and I think this might even be a setup issue, we can still improve the code and
make sure to not call `hasField` method if the variable is not the correct type.

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Fixed

Version

12.0

Component

Code (back-end)

Created by

🇸🇮Slovenia nkoporec Slovenia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024