Q: Error: getParentCategories() on null I am working on my custom module where I added a button on Configurable products. The Content I was showing through button was by getting the Parent Category ID. 
The main issue I am facing is that when I open a product by going through the path(mean Men>Tops>Jackets) it works. But When I directly open the product from HOME PAGE it gives me the error.

In my Block file, I am using these functions
public function getCurrentCategory()
    {   

            return $this->_registry->registry('current_category');
    }

     public function getCurrentProduct()
    {       

        return $this->_registry->registry('current_product');
    }

    public function getCollection($id){
        return $this->_Collection->create()
                                ->addFieldToSelect('content')
                                ->addFieldToFilter('parent_category',$id)
                                ->getData();
    } 

    public function getDisplay($id){
        return $this->_Collection->create()
                                ->addFieldToSelect('display')
                                ->addFieldToFilter('parent_category',$id)
                                ->getData();
    }

For my phtml file, I am using
$cat = $block->getCurrentProduct()->getCategory(); 

    if($block->getCurrentProduct())

    {
         foreach ($cat->getParentCategories() as $parent) {
            if($cat)
            {
            $parentId[]= $parent->getId();
        }}

        $sizeColl = $block->getDisplay($parentId[0]);
        if($sizeColl){
            $block->getCurrentCategory()->getId();
            $display = $sizeColl[0]['display'];

            if($display == 'page'): 
                $Charts=$block->getCollection($parentId[0]); 
                        foreach ($Charts as $chart) {
                            echo $chart['content'];
                        }

            else: ?>

Can Anyone help me out with this or suggest me with a better option. Because I need to target Main Parent Category to do my work. Thanks in advance 
