tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/679-injection-error-when-injecting-one-actor-into-anotherRobotlegs: Discussion 2012-11-20T10:11:44Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/208565542012-11-15T10:26:30Z2012-11-15T10:26:30ZInjection error when injecting one Actor into another<div><p>Hi Josh,</p>
<p>Of course it would be good to see some code: mappings,
injections into your actor class…, what does your factory
class look like?<br>
From the error you’re getting, I guess it has something to do
with some sort of constructor injection.</p>
<p>First take a look at these:</p>
<p><a href=
"https://github.com/robotlegs/robotlegs-framework/wiki/common-problems#wiki-no-injection">
https://github.com/robotlegs/robotlegs-framework/wiki/common-proble...</a></p>
<p><a href=
"http://shaun.boyblack.co.za/blog/2009/05/01/constructor-injection-vs-setter-injection/">
http://shaun.boyblack.co.za/blog/2009/05/01/constructor-injection-v...</a></p>
<p>Some questions:</p>
<p>Is your Model mapped as a singleton?<br>
Are you injecting it using [Inject]?<br>
Are you creating a new instance of that Model?<br>
Are you using setters and getters?<br>
Are you accessing the Model in the constructor of the other
class?</p>
<p>If you can’t figure it out, just paste some relevant code
or attach a simplified example reproducing the issues.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/208565542012-11-15T16:32:29Z2012-11-15T16:32:29ZInjection error when injecting one Actor into another<div><p>To sum it up:</p>
<p>1- Check if the classes that need to be injected have been
mapped<br>
<strong>No mapping=No Injection</strong> (Injector is missing a
rule to handle injection into target)</p>
<p>2- Make sure the mappings occur before the injection<br>
<strong>Mapping first, Injection second</strong> (Cannot access a
property or method of a null object reference.)</p>
<p>3- Make sure you have the <strong>correct injection point
declaration</strong> / see Common Problems: Incorrect Injection
Point Declaration</p>
<p>4- Make sure the class needing an injection has been constructed
before trying to access the injected properties –
<strong>properties are null in the constructor</strong></p>
<p>5- Try to avoid doing work in constructors</p>
<p>Situations where <strong>injection doesn’t
work</strong>:</p>
<h2>A.</h2>
<p>Accessing the injected properties in the constructor</p>
<p>Context:<br>
injector.mapSingleton( AnotherModel);//to be injected into
SomeModel<br>
injector.mapSingleton(SomeModel);</p>
<p>SomeModel.as</p>
<pre>
<code>[Inject]
public var _anotherModel:AnotherModel;
public function SomeModel()
{
trace(_anotherModel.aProperty);
}</code>
</pre>
<p>AnotherModel is null in the constructor (Error: Cannot access a
property or method of a null object reference.)</p>
<pre>
<code>[PostConstruct]
public function someInit():void
{
trace(anotherModel.aProperty);
}</code>
</pre>
<p>In someInit, AnotherModel is not null.</p>
<h2>B.</h2>
<p>There are no mappings for AnotherModel in the context, and you
try to pass AnotherModel as a parameter on to SomeModel’s
constructor, but constructor injection fails without mapping
AnotherModel in the context.</p>
<pre>
<code>public class SomeCommand extends Command
{
public var anotherModel:AnotherModel;//no inject tag
[Inject]
public var someModel:SomeModel;
override public function execute():void
{
anotherModel=new AnotherModel();
anotherModel. aProperty ="data set in a command";
someModel =new SomeModel (anotherModel);
someModel.someMethod("someString");
}</code>
</pre>
<p>SomeModel.as</p>
<pre>
<code>public var _anotherModel:AnotherModel;//no inject tag
public function SomeModel(anotherModel:AnotherModel)
{
_anotherModel= anotherModel;
trace(_anotherModel.aProperty);
}
[PostConstruct]
public function someInit():void
{
_anotherModel.someData="data set in a post construct method";
trace ("someInit"+_anotherModel. aProperty);
}
public function someMethod(value:String):void
{
trace("someMethod "+_anotherModel. aProperty);
}</code>
</pre>
<h2>Error: Injector is missing a rule to handle injection into
property "anotherModel" …</h2>
<p>What I would do:<br>
1-Context:<br>
injector.mapSingleton( AnotherModel);//to be injected into
SomeModel<br>
injector.mapSingleton(SomeModel);<br>
2- trigger the command after mappings are done<br>
3- Command</p>
<pre>
<code>public class SomeCommand extends Command
{
[Inject]
public var anotherModel:AnotherModel;
[Inject]
public var someModel:SomeModel;
override public function execute():void
{
anotherModel. aProperty ="data set in a command";
someModel.someMethod("someString");
}</code>
</pre>
<p>4-Model<br>
SomeModel.as</p>
<pre>
<code>[Inject]
public var _anotherModel:AnotherModel;
public function SomeModel()
{
//**NOTHING**
}
[PostConstruct]
public function someInit():void
{
//I wouldn’t use PostConstruct, but it’s a possible solution
_anotherModel.someData="data set in a post construct method";
trace ("someInit"+_anotherModel. aProperty);
}
public function someMethod(value:String):void
{
trace("someMethod "+_anotherModel. aProperty);
_anotherModel. aProperty=value;
}</code>
</pre>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/208565542012-11-15T18:00:06Z2012-11-15T18:00:08ZInjection error when injecting one Actor into another<div><p>Thanks, Ondina, for the long reply.</p>
<p>To answer your questions:<br>
1) Is your Model mapped as a singleton? YES<br>
2) Are you injecting it using [Inject]? YES<br>
3) Are you creating a new instance of that Model? NO<br>
4) Are you using setters and getters? YES, in the model that is
being injected. Is this an issue? I have another Model being
injected into this Factory class, and that one also has get/set
methods, and that injection is not throwing errors, and the getter
method is being used in the class it's injected into. I'm doubting
that's the issue.<br>
5) Are you accessing the Model in the constructor of the other
class? NO. As a rule, I stay away from code in constructors.</p>
<p>Here's a look at the injection:</p>
<pre>
<code>public final class FilmDetailsParser extends Actor {
//[Inject]
//public var applicationStateModel:ApplicationStateModel;
[Inject]
public var remoteControlMenuModel:RemoteControlMenuModel;
private var _exhibition: ExhibitionVo;
private var _title: TitleVo;
// LoaderMax instance
private var _actorsXMLArray: Array;
private var _writersXMLArray: Array;
private var _directorsXMLArray: Array;
private var _loaderQueue: LoaderMax;
private var _actors: Vector.<PersonVo>;
private var _writers: Vector.<PersonVo>;
private var _directors: Vector.<PersonVo>;
private var _genres: Vector.<GenreVo>;
private var _ratings: Vector.<RatingVo>;
public function FilmDetailsParser() {}</code>
</pre>
<p>Note, that if the injection of "applicationStateModel" above is
uncommented, this is when the error occurs.</p>
<p>This is how the injected Model needs to be used:</p>
<pre>
<code>var stateObject:Object = applicationStateModel.stateObject;
// This is via public getter on the ApplicationStateModel for private var _stateObject</code>
</pre>
<p>Of note is that the error occurs virtually immediately upon
attempting to run the SWF, and the error is also shown as
"undefined", with no indication of a class file that contains the
error (at least not outside of RL core files) nor any of the normal
Injection errors.</p></div>Joshtag:robotlegs.tenderapp.com,2009-10-18:Comment/208565542012-11-15T18:36:53Z2012-11-15T18:36:53ZInjection error when injecting one Actor into another<div><p>Hey Josh,</p>
<p>I’m about to go offline, so I don’t have much time
for investigating your code.<br>
Some quick thoughts, though:<br>
Is applicationStateModel.stateObject something you’re
innjecting into ApplicationStateModel or is it a <em>normal</em>
property?<br>
Maybe you forgot to map it as an interface, in case
ApplicationStateModel is implementing one?</p>
<p>Can you paste some code from ApplicationStateModel? I guess,
there is something not quite right.</p>
<p>Till tomorrow..<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/208565542012-11-15T18:49:05Z2012-11-15T18:49:05ZInjection error when injecting one Actor into another<div><p>applicationStateModel.stateObject is a private variable on
ApplicationStateModel with a public getter method. I can paste some
code from the ApplicationStateModel, but there's not a lot there
that will be helpful. I feel as though there is some sort of
circular reference going on here, where one class is injecting
another is injecting another and it can't sort out the chain, but
I'm not sure what that would be.</p></div>Joshtag:robotlegs.tenderapp.com,2009-10-18:Comment/208565542012-11-16T11:01:40Z2012-11-16T11:01:40ZInjection error when injecting one Actor into another<div><p>Hi Josh,</p>
<blockquote>
<p>I feel as though there is some sort of circular reference going
on here, where one class is injecting another is injecting another
and it can't sort out the chain, but I'm not sure what that would
be.</p>
</blockquote>
<p>Oh, yes, that could cause problems. But without seeing the code
I can’t help you.<br>
There are 2 options:<br>
-Either you figure it out by yourself, maybe through using the
debugger, trace or log statements, or by reducing the complexity of
the classes in question and adding functionalities one by one</p>
<p>-Or, you can attach the app, and I can take a look at it. If you
want, I (or you) can make the discussion private and after I dl-ed
the code I delete the attachment and make the discussion public
again.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/208565542012-11-20T10:11:44Z2012-11-20T10:11:44ZInjection error when injecting one Actor into another<div><p>Josh, I guess you solved your problem, thus I’m closing
the discussion for now, but you can re-open it, if need be.</p></div>Ondina D.F.