tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/8661-injection-in-base-class-not-available-in-sub-classRobotlegs: Discussion 2013-12-16T15:25:06Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/305231942013-12-12T16:27:32Z2013-12-12T16:27:33ZInjection in base-class not available in sub-class<div><p>Hello! Sorry for my "bad english".<br>
I have some problems with using of result injection in sub class
from base class.</p>
<p>In config class i have this fragment of code:<br></p>
<pre>
<code>...
injector.map(ApplicationCacheFacade).asSingleton();
injector.map(ApplicationLoadersFacade).asSingleton();
...</code>
</pre>
<p>Part of base class:<br></p>
<pre>
<code>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 = <strong>cacheFacade</strong>.getCache(cacheClass) as AbstractCache;
cache.loadCache();
loader = new loaderClass(loaderFacade.cachable, cache);
}
...
}</code>
</pre>
<p>Sub class:<br></p>
<pre>
<code> public class XMLLoaderAdapter extends ItemLoaderAdapter
{
public function XMLLoaderAdapter()
{
cacheClass = CacheFabrics.XMLs;
loaderClass = XMLItemLoader;
super();
}
}</code>
</pre>
<p>In command i try create object of XMLLoaderAdapter:<br></p>
<pre>
<code>public class ApplicationStartedCommand extends Command
{
override public function execute():void
{
var loader:XMLLoaderAdapter = new XMLLoaderAdapter();
}
}</code>
</pre>
<p>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 <strong>cacheFacade</strong> object.<br>
<strong>cacheFacade</strong> is null</p>
<p>Please tell me what I'm doing wrong. Thank you!</p></div>condorcettag:robotlegs.tenderapp.com,2009-10-18:Comment/305231942013-12-12T22:30:16Z2013-12-12T22:45:26ZInjection in base-class not available in sub-class<div><p>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.</p>
<ol>
<li>
<p> you are trying to access the injected classes in the
constructor. See this:<br>
Injected Properties Are Null In Constructor<br>
<a href=
"https://github.com/robotlegs/robotlegs-framework/wiki/common-problems#wiki-null-in-constructor">
https://github.com/robotlegs/robotlegs-framework/wiki/common-proble...</a></p>
</li>
<li>
<p> you are creating a new instance of XMLLoaderAdapter,
which is not known by the injector:<br>
var loader:XMLLoaderAdapter = new
XMLLoaderAdapter();</p>
</li>
</ol>
<p> XMLLoaderAdapter has to be instantiated by the injector,
if you want cacheFacade and loaderFacade to be injected into
it</p>
<p>You could map XMLLoaderAdapter as a singleton and then
inject it into your command.</p>
<p>See: <a href=
"http://knowledge.robotlegs.org/discussions/robotlegs-2/8182-the-necessary-conditions-of-injection-to-work">
http://knowledge.robotlegs.org/discussions/robotlegs-2/8182-the-nec...</a>
, my answer there:</p>
<p>
<a> http://knowledge.robotlegs.org/discussions/robotlegs-2/8182-the-ne...</a></p>
<p><a href=
"http://knowledge.robotlegs.org/discussions/robotlegs-2/5766-automagic-view-injection#comment_28759520">
http://knowledge.robotlegs.org/discussions/robotlegs-2/5766-automag...</a></p>
<p>Let me know if that solved your issue and/or if you need more
explanations.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/305231942013-12-15T14:07:27Z2013-12-15T14:07:27ZInjection in base-class not available in sub-class<div><p>Thank you for the answer, it is enough to solve my problem
:)</p></div>condorcettag:robotlegs.tenderapp.com,2009-10-18:Comment/305231942013-12-16T15:24:15Z2013-12-16T15:24:15ZInjection in base-class not available in sub-class<div><p>My pleasure:)</p></div>Ondina D.F.