cancel
Showing results for 
Search instead for 
Did you mean: 

Call to a member function get() on null in ObjectManager/Factory/AbstractFactory.php

Call to a member function get() on null in ObjectManager/Factory/AbstractFactory.php

Hi, 

 

I have created magento 2 module (a Magento2 Cron job, no Web stuff). Before it gets fully autogenerated, i.e. before the cron job Synchronize->execute() is even started, the whole thing runs into a problem.

 

The trace shows that the problem emerges preparing dependency injection  for Panlab\Panstore\Model\Producer. This is my class ... that is the Panlab\Panstore things are my module.

 

However, it seems as though the problem pertains to internals of ObjectManager factory dynamic generation. You can see the trace here.

 

I am sure the problem is on my side; however, the trace gives no clues as to what I am actually doing wrong. Perhaps I maintain a class in a namespace where it shouldn't be ... Any help would be appreciated.

 

Thanks

6 REPLIES 6

Re: Call to a member function get() on null in ObjectManager/Factory/AbstractFactory.php

I think I have found the reason for the problem: in the ./Model subsection of your module, there should be nothing else but the objects which are coupled with appropriate ResourceModel clases (model + collection folders).

 

If this thing is missing, you will run into the same kind of a problem as I have ... dynamic generator does not find the expected classes in ResourceModel and conveys NULL instead of definite objects in certain dependency injection procedures.

Re: Call to a member function get() on null in ObjectManager/Factory/AbstractFactory.php

Unfortunately this did not solve the problem I still have the same error. The trace is different, but the error the same. This is the full PHP error log ... please search for "PHP Fatal error" ... any help appreciated ...

Re: Call to a member function get() on null in ObjectManager/Factory/AbstractFactory.php

I have produced a minimal codesample for my problem. You can view it here. It is a very small Magento 2 module implementation (only a few lines). If you run it inside app/code on a fresh Mageno 2 installation, you will get this error

 

PHP Fatal error:  Call to a member function get() on null in /var/www/jesej.si/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 126

 

Anyone understands why ?

 

P.S. Please check also the full transcript of the PHP error log after running the module.

Re: Call to a member function get() on null in ObjectManager/Factory/AbstractFactory.php

By systematically eliminating segments of code, I have actually produced the absolute minimal model which results in my problem. The minimal model (disregarding registration.php and etc/ stuff) is:

class Synchronize {
protected $objectManager;
__construct(\Magento\Framework\App\ObjectManager $objectManager) {
$this->objectManager = $objectManager;
}
}

Put that class as the only class in the root folder of the model in Synchronize.php, run the magento cron ... and baaaam => you get the null pointer exception.

It is obvious that my module has nothing to do with it. The question is, why dependency injection is not working fo ObjectManager ... interestingly, only for object manager, because I was able to use this approach to successfuly inject dependency for a plethora of other Magento2 stuff (indexers ...). IMHO this is Magento2 bug.

I was able to circumvent this problem by

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

This works perfectly.

Re: Call to a member function get() on null in ObjectManager/Factory/AbstractFactory.php

Thanks for submitting the minimal module. I'll take a look if this is understood behavior or a bonafide bug. In the former case, we might want some documentation over in DevDocs, and in he latter case a report should be filed on GitHub

-----
@benmarks, Senior Manager, Strategy & Growth

Re: Call to a member function get() on null in ObjectManager/Factory/AbstractFactory.php

Don't inject the OM directly

 

\Magento\Framework\App\ObjectManager

but instead its interface

 

\Magento\Framework\ObjectManagerInterface $objectmanager

.