Stack Exchange
Getting "Call to a member function getName() on null" in custom breadcrumb
I'm trying to customize the breadcrumb of drupal 8 to get something like this:
Home > category >article-title.
I wrote this and I wrote the code below.
namespace Drupal\module_demo\Breadcrumb;

use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Link;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\taxonomy\Entity\Term;

class ArticleBreadcrumbBuilder implements BreadcrumbBuilderInterface {

    public function applies(RouteMatchInterface $route_match) {
        $parameters = $route_match->getParameters()->all();
        if(isset($parameters['node'])) {
            return $parameters['node']->getType() === 'article';
        }

    }

    public function build(RouteMatchInterface $route_match) {

        $breadcrumb = new Breadcrumb();

        $term = $route_match->getParameter('taxonomy_term');

        $breadcrumb->addCacheContexts(["url"]);

        $breadcrumb->addLink(Link::createFromRoute(t('Home'), '<front>'));

        $breadcrumb->addLink(Link::createFromRoute(t('Articles'), '<none>'));

        $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]));

        $request = \Drupal::request();
        $route_match = \Drupal::routeMatch();
        $page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
        if (!empty($page_title)) {
            $breadcrumb->addLink(Link::createFromRoute($page_title, '<none>'));
        }

        return $breadcrumb;
    }
}

but I have the error below.

The website encountered an unexpected error. Please try again later.
  Error: Call to a member function getName() on null in Drupal\module_demo\Breadcrumb\ArticleBreadcrumbBuilder->build() (line 34 of modules/custom/module_demo/src/Breadcrumb/ArticleBreadcrumbBuilder.php). 

Apparently the problem is with getName() and id().
Can someone tell me where the mistake is?

You can also use hook_system_breadcrumb_alter to achieve this:
Try this:
/**
 * Implements hook_system_breadcrumb_alter().
 */
function MyModule_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context) {
  $node = \Drupal::request()->attributes->get('node');
  if ($node && $node instanceof Drupal\node\Entity\Node) {
    if ($node->bundle() == 'article') {
      // Add Articles item.
      $breadcrumb->addLink(Link::createFromRoute(t('Articles'), '<none>'));
      // Add category term item to breadcrumb.
      // Change here the field_tags with your taxonomy you want to add to breadcrumb.
      $term_id = $node->field_tags->getValue()[0]['target_id'];
      $term = Term::load($term_id);
      $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]));
      // Add title as breadcrumb item.
      $request = \Drupal::request();
      $route_match = \Drupal::routeMatch();
      $page_title = \Drupal::service('title_resolver')
        ->getTitle($request, $route_match->getRouteObject());
      if (!empty($page_title)) {
        $breadcrumb->addLink(Link::createFromRoute($page_title, '<none>'));
      }
    }
  }
}

Update: To fix the error.
Update the applies method to apply your breadcrumb only on articles as it mentioned in @Jdrupal answer:
  public function applies(RouteMatchInterface $route_match) {
    $parameters = $route_match->getParameters()->all();
    if (isset($parameters['node'])) {
      return $parameters['node']->getType() === 'article';
    }
    if (isset($parameters['term'])) {
      return TRUE;
    }
  }

The error comes from the line:
$term = $route_match->getParameter('taxonomy_term');

$term equal NULL its normal, you are in node page so the taxonomy_term param always null, so try to get your term from the node object something like:
 $node = \Drupal::request()->attributes->get('node');
 // Change the field_tags with your filed.
 $term_id = $node->field_tags->getValue()[0]['target_id'];
 $term = Term::load($term_id);


Hi @berramou, Thank you. Your solution works perfectly. Thank you very much.

But tell me please. I was talking about taxonomy term. It's rather the vocabulary I want. Can you tell me how to get in the code that you put the vocabulary instead of the taxonomy term.

I don't understand what you want to achieve exactly but if you want to get a vocabulary you can use something like $vocabulary = \Drupal\taxonomy\Entity\Vocabulary::load('tags');    $vocab_name = $vocabulary->get('name');

Excuse me very much. I'm new and I think I'm mixing up the brushes. In fact, the code you made works perfectly. But after putting it on, I noticed that it was the first term of the tags that appeared (I finally understood by looking at the code). But in fact, I created in drupal two vocabularies called "Articles category" and which contain 2 terms (vocab1, vocab2). It is these terms that I want breadcrumb to display (not the name of the vocabulary itself).

It's okay, i get now, you want to display all terms of a vocabulary right?

No, I want to display only one tag. But a tag that belongs to the vocabulary "articles category". I think I just have to replace field_tags in your code with the tag name of my vocabulary. That's what you said in your code. But when I do, I get a error.

take a look at this https://drupal.stackexchange.com/questions/144147/get-taxonomy-terms

You create another taxonomy like Tags from here admin/structure/taxonomy and you added to it another terms from here admin/structure/taxonomy/manage/YOUR_VOCAB/overview e.g (vocab1, vocab2) and you want the breadcrumb to be Home > articles > vocab1 >vocab2 >article-title. right ?

No I want Home > articles > vocab1 (or vocab2 according to the article) > article-title . I created the Tags. It's categories_articles. Its terms are "vocab1", "vocab2". I followed the solution you shared with me. It seems to work, except it displays the same term vocab1 in the breadcrumb of all articles.

Hi @berramou Sorry but as I thought, your initial answer solved my problem very well. All I had to do was replace field_tags in the code to get $term_id = $node->field_categorie->getValue()[0]['target_id'];. And that's okay. Instead, I replaced it with the machine name of my vocabulary. I was really very stupid.  I was sure you've already answered my question. I thought a little bit about your code and finally understood. Thank you very much for your patience. I learn a little more every day.

You are welcome @Worksfather i'm glad that i helped.

Thanks. but I have one last question, please :) . If finally, I want to display the same breadcrumb trail for all "node" and "not node" pages, I change my code where? I tried to do so and I noticed the page title does not appear for the other pages (it only appears for pages with the node).

Let us continue this discussion in chat.

The problem is that you try to get the term from the route, but in your applies() method you have restricted the breadcrumb to apply only on pages where you can get the node from the route.
If you want your breadcrumb to apply for taxonomy term pages as well your applies() function must look like this:
public function applies(RouteMatchInterface $route_match) {
  $parameters = $route_match->getParameters()->all();
  if (isset($parameters['node'])) {
    return $parameters['node']->getType() === 'article';
  }
  if (isset($parameters['term'])) {
    return TRUE;
  }
}

Otherwise you have to remove $term->getName() from your build() method.
UPDATE: Since you mention you only want to display the breadcrumb on article pages, you must fetch the term from the node.
Replace this line:
$term = $route_match->getParameter('taxonomy_term');

With:
$term = $route_match->getParameter('node')->field_term->first()->entity;

Then you can call $term->label().

Hi @Jdrupal. I don't know if I don't understand what you said. But what I want is to display the breadcrumb for only article type nodes. But in the breadcrumb I want the category to appear. As Home > Category > title-article

Hi, I have updated my answer.

I just tried. The problem persists.

Try label() instead. Remember you have to replace field_term with the fieldname of your term field.

Your applies() checks for a node-route while your build() wants a taxonomy-route. That's why $term is null here.
You have to fetch your $term object from the corresponding node field.

Thank you @Rainer. I tried to do it, but it still doesn't work. I'm new to Drupal. Can you tell me exactly what part of my code I need to change please?
