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-&gt;getParameters()-&gt;all();
        if(isset($parameters['node'])) {
            return $parameters['node']-&gt;getType() === 'article';
        }

    }

    public function build(RouteMatchInterface $route_match) {

        $breadcrumb = new Breadcrumb();

        $term = $route_match-&gt;getParameter('taxonomy_term');

        $breadcrumb-&gt;addCacheContexts(["url"]);

        $breadcrumb-&gt;addLink(Link::createFromRoute(t('Home'), '&lt;front&gt;'));

        $breadcrumb-&gt;addLink(Link::createFromRoute(t('Articles'), '&lt;none&gt;'));

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

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

        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 &amp;$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context) {
  $node = \Drupal::request()-&gt;attributes-&gt;get('node');
  if ($node &amp;&amp; $node instanceof Drupal\node\Entity\Node) {
    if ($node-&gt;bundle() == 'article') {
      // Add Articles item.
      $breadcrumb-&gt;addLink(Link::createFromRoute(t('Articles'), '&lt;none&gt;'));
      // Add category term item to breadcrumb.
      // Change here the field_tags with your taxonomy you want to add to breadcrumb.
      $term_id = $node-&gt;field_tags-&gt;getValue()[0]['target_id'];
      $term = Term::load($term_id);
      $breadcrumb-&gt;addLink(Link::createFromRoute($term-&gt;getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' =&gt; $term-&gt;id()]));
      // Add title as breadcrumb item.
      $request = \Drupal::request();
      $route_match = \Drupal::routeMatch();
      $page_title = \Drupal::service('title_resolver')
        -&gt;getTitle($request, $route_match-&gt;getRouteObject());
      if (!empty($page_title)) {
        $breadcrumb-&gt;addLink(Link::createFromRoute($page_title, '&lt;none&gt;'));
      }
    }
  }
}


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-&gt;getParameters()-&gt;all();
    if (isset($parameters['node'])) {
      return $parameters['node']-&gt;getType() === 'article';
    }
    if (isset($parameters['term'])) {
      return TRUE;
    }
  }


The error comes from the line:

$term = $route_match-&gt;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()-&gt;attributes-&gt;get('node');
 // Change the field_tags with your filed.
 $term_id = $node-&gt;field_tags-&gt;getValue()[0]['target_id'];
 $term = Term::load($term_id);