Injection in base-class not available in sub-class

condorcet's Avatar

condorcet

12 Dec, 2013 04:27 PM

Hello! Sorry for my "bad english".
I have some problems with using of result injection in sub class from base class.

In config class i have this fragment of code:

...
injector.map(ApplicationCacheFacade).asSingleton();
injector.map(ApplicationLoadersFacade).asSingleton();
...

Part of base class:

public class ItemLoaderAdapter extends EventDispatcher
{
    [Inject]
    public var cacheFacade:ApplicationCacheFacade;
...
    [Inject]
    public var loaderFacade:ApplicationLoadersFacade;
...
    public function ItemLoaderAdapter()
    {
        init();
    }
    protected function init():void
    {
        if (!cacheClass)
            throw new Error('cache-class not defined');
        if (!loaderClass)
            throw new Error('loader-class not defined');
        cache = cacheFacade.getCache(cacheClass) as AbstractCache;
        cache.loadCache();
        loader = new loaderClass(loaderFacade.cachable, cache);
    }
...
}

Sub class:

 public class XMLLoaderAdapter extends ItemLoaderAdapter 
    {
        public function XMLLoaderAdapter() 
        {
            cacheClass = CacheFabrics.XMLs;
            loaderClass = XMLItemLoader;
            super();
        }
    }

In command i try create object of XMLLoaderAdapter:

public class ApplicationStartedCommand extends Command 
{
    override public function execute():void
    {
        var loader:XMLLoaderAdapter = new XMLLoaderAdapter();
    }
}

But get the following error from init() method of base-class: "Cannot access a property or method of a null object reference.", with reference on cacheFacade object.
cacheFacade is null

Please tell me what I'm doing wrong. Thank you!

  1. Support Staff 1 Posted by Ondina D.F. on 12 Dec, 2013 10:30 PM

    Ondina D.F.'s Avatar

    I'm about to go to bed, so I'll give you just a short answer now, and I'll explain more tomorrow, if need be.

    1.  you are trying to access the injected classes in the constructor. See this:
      Injected Properties Are Null In Constructor
      https://github.com/robotlegs/robotlegs-framework/wiki/common-proble...

    2.  you are creating a new instance of XMLLoaderAdapter, which is not known by the injector:
              var loader:XMLLoaderAdapter = new XMLLoaderAdapter();

     XMLLoaderAdapter has to be instantiated by the injector, if you want cacheFacade and  loaderFacade to be injected into it

    You could map  XMLLoaderAdapter as a singleton and then inject it into your command.

    See: http://knowledge.robotlegs.org/discussions/robotlegs-2/8182-the-nec... , my answer there:

     http://knowledge.robotlegs.org/discussions/robotlegs-2/8182-the-ne...

    http://knowledge.robotlegs.org/discussions/robotlegs-2/5766-automag...

    Let me know if that solved your issue and/or if you need more explanations.

    Ondina

  2. 2 Posted by condorcet on 15 Dec, 2013 02:07 PM

    condorcet's Avatar

    Thank you for the answer, it is enough to solve my problem :)

  3. Support Staff 3 Posted by Ondina D.F. on 16 Dec, 2013 03:24 PM

    Ondina D.F.'s Avatar

    My pleasure:)

  4. Ondina D.F. closed this discussion on 16 Dec, 2013 03:24 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac