Magento – Fatal error: Cannot instantiate interface in Magento 2

interfacemagento2object-manager

As the part of creating bundle product programmatically in Magento 2.
I am getting below error. Even i removed var/cache and generation.

This may be a duplicate question but none of the answer is working for this issue.

Fatal error: Cannot instantiate interface
Magento\Catalog\Api\Data\ProductExtensionInterface in
\ObjectManager\Factory\Dynamic\Developer.php on line 73

Used below code as the reference of link

$productExtension = $objectManager->create(\Magento\Catalog\Api\Data\ProductExtensionInterface::class);
$option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterface::class);
$option->setTitle('Size');
$option->setType('radio');
$option->setRequired(true);
$option->setPosition(1);
//$option->setProductLinks($links);
$productExtension->setBundleOptions([$option]);
$_product->setExtensionAttributes($productExtension);

I am using object manager for temporary/quick purpose, will convert the object manager to constructor injunction.

Any one can help me on this issue.

Best Answer

I think you have not mentioned the preference in your di.xml. I also faced the same issue when I was creating custom config xml file module.

I got the issue when I called this code:

$testConfig = $this->_objectManager->get('Training\Test\Model\Config\ConfigInterface');
$myNodeInfo = $testConfig->getMyNodeInfo();

Below is the sample code I have added in my di.xml, may be it may help you to get reference:

<preference for="Training\Test\Model\Config\ConfigInterface" type="Training\Test\Model\Config" />

Here I have implemented the interface in Training\Test\Model\Config class and my getMyNodeInfo() method is defined in that Model.

So based on the the preference defined in di.xml the code ->get('Training\Test\Model\Config\ConfigInterface'); will create an object of type Training\Test\Model\Config.

Related Topic