Magento2: PHP Fatal error: Call to a member function provide() on null in /home/campuxsy/public_html/vendor/magento/module-swatches/Helper/Data.php on line 349

Created on 1 Aug 2017  路  8Comments  路  Source: magento/magento2

hi,
using magento 2.1.7
I installed a third party ecommerce template and get similar problem ie.
PHP Fatal error: Call to a member function provide() on null in /home/XXXXXXX/public_html/vendor/magento/module-swatches/Helper/Data.php on line 349
screen shot 2017-08-01 at 8 41 19 am

Format is not valid

All 8 comments

.

How did you solve this?

I'm having this same problem, please post your solution!

Yeah. i m still looking out for solutions. I don't know what the problem is.
I make changes from the product attribute from drop down type to Visual Swatch type. Then this error occurs. I switched it back to drop down type, but the problem still exist. So weird.

Fixed it with a core hack.
Above:

$swatchAttributes = $this->swatchAttributesProvider->provide($product);

Put this:

if (!isset($this->swatchAttributesProvider)) { $this->swatchAttributesProvider = ObjectManager::getInstance()->get(SwatchAttributesProvider::class); }

@bruno-mspan how do you reproduce this error if such assignment is already in constructor https://github.com/magento/magento2/blob/2.1.7/app/code/Magento/Swatches/Helper/Data.php#L103 ?

@orlangur sorry but I don't know, just hope this helps someone who finds themself in the same situation as I did. I can only guess as to why this was an issue in the first place.

In my case, Magento 2.2.3 is showing similar error with serialize object.
Both $serializer and $provider variables are being checked in constructor of the class vendor/magento/module-swatches/Helper/Data.php to be initialised with the help of ObjectManager if found uninitialised, but ternary operator in php ? : is not evaluating null property properly. There are two fixes to this problem:

  • 1

Fix the ternary operator evaluation part like:
$this->serializer = isset($serializer) ?: ObjectManager::getInstance()->create(Json::class);

  • 2

    Check them and initialise them explicitly before they are being used in the class.
    In my case, I am getting error on line 148 of the class. So this hack works fine:
    if(null == $this->serializer) { $this->serializer = ObjectManager::getInstance()->create(Json::class); }
    before the following line:
    $attribute->setData('additional_data', $this->serializer->serialize($additionalData));

and similar case with the $provider and other variables if Magento shows similar error for them.

Please Note: Do not hack this class in the vendor folder, rather override it in your own code.

Was this page helpful?
0 / 5 - 0 ratings