tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/345-incjected-model-singleton-equals-nullRobotlegs: Discussion 2018-10-18T16:35:29Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/85028582011-07-09T18:51:16Z2011-07-09T18:52:04Zinjected model (singleton) equals null<div><p>In main context:</p>
<p>injector.mapSingletonOf( IWorldModel, WorldModel );</p>
<p>In some command:<br>
(it works fine)</p>
<p>[Inject] public var worldModel :IWorldModel;</p>
<p>In another model:<br>
( this doesn't work, worldModel == null )</p>
<p>[Inject] public var worldModel :IWorldModel;</p>
<p>What could I be doing wrong?</p></div>patryktag:robotlegs.tenderapp.com,2009-10-18:Comment/85028582011-07-09T18:56:27Z2011-07-09T18:56:27Zinjected model (singleton) equals null<div><p>Hi patryk - on the front of the forum is a link to our 'common
problems' documentation - that has a great section all about null
injector errors - it's usually one of half a dozen simple things,
so that's the place to start.</p>
<p>In a nutshell:</p>
<ul>
<li>typo in [Inject]</li>
<li>race conditions (the model needing the mapping is asking for it
before the mapping has been made)</li>
<li>accidental ; after [Inject]</li>
</ul>
<p>hth,</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/85028582011-07-09T19:10:15Z2011-07-09T20:36:03Zinjected model (singleton) equals null<div><p>Thank you for a quick reply Stray.</p>
<p>No typos, mapping is done before injection...<br>
I just checked the common problems doc, and I can not find anything
wrong in the code.</p>
<p>public class WorldModel extends Actor implements IWorldModel<br>
{ private var robotVector :Vector. = new Vector.;<br>
. . . }.</p>
<p>public function addRobot( robotModel:IRobotModel ):void<br>
{</p>
<pre>
<code>robotModel.grid = getGrid( robotModel.robotView );
robotVector[ robotVector.length ] = robotModel;
dispatch( new RobotEvent( RobotEvent.ROBOT_CREATED, robotModel ) );</code>
</pre>
<p>}</p>
<p>------ that RobotEvent is mapped to DisplayRobotCommand:</p>
<p>public class DisplayRobotCommand extends Command<br>
{</p>
<pre>
<code> [Inject]
public var worldModel :IWorldModel;
override public function execute():void
{
worldModel.world.add( event.robotModel.robotView ); /// HERE THE INJECTION WORKS
}</code>
</pre>
<p>}</p>
<p>-------- and then in WorldModel:</p>
<p>private function onTimer( e:TimerEvent ):void<br>
{ for ( var i:int = 0; i < robotVector.length; i++ )
robotVector[ i ].oEF();<br>
}</p>
<p>---------- and inside that robotModel:</p>
<p>public class PlayerRobotModel implements IRobotModel</p>
<pre>
<code>{
[Inject]
public var worldModel :IWorldModel;
private var _robotView :RobotView;
public function oEF():void
{
MonsterDebugger.trace( this, worldModel, "worldModel"); ////// AND HERE COMES THE ERROR:</code>
</pre>
<p>TypeError: Error <a href="/discussions/problems/1009" title=
"Discussion #1009">#1009</a>: Cannot access a property or method of
a null object reference.</p>
<p>What am I doing something wrong here?</p>
<p>BTW, What tags can I use to format the code in this post?</p></div>patryktag:robotlegs.tenderapp.com,2009-10-18:Comment/85028582011-07-09T21:02:28Z2011-07-09T21:02:28Zinjected model (singleton) equals null<div><p>Hi Patryk, the things in common problems, plus the race
conditions, are the only things I've ever heard of being
problems.</p>
<p>But please do paste your code - I won't get a chance to look
before tomorrow now (10pm here in uk) but hopefully someone else
will :)</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/85028582011-07-09T21:09:43Z2011-07-09T21:17:44Zinjected model (singleton) equals null<div><p>Thanks a lot Stray.</p>
<p>How can I get this trace of the order of mapping, etc ?<br>
I have seen it somewhere on this forum but can't find it.</p>
<p>BTW: 10pm here too, in Southampton :)</p></div>patryktag:robotlegs.tenderapp.com,2009-10-18:Comment/85028582011-07-09T22:40:30Z2011-07-09T22:40:30Zinjected model (singleton) equals null<div><p>How is PlayerRobotModel being created? You're not doing new
PlayerRobotModel() are you?</p>
<p>Just a thought... the injector has to be used to create any
instance requiring injections.</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/85028582011-07-09T22:45:37Z2011-07-09T22:45:37Zinjected model (singleton) equals null<div><p>I am doing this in one of the commands:</p>
<p>dispatch( new RobotEvent( RobotEvent.CREATE_ROBOT, new
PlayerRobotModel( new RobotView( 4, 0xFF3399, 4, 30, 290, 20 ) ) )
);</p></div>patryktag:robotlegs.tenderapp.com,2009-10-18:Comment/85028582011-07-09T22:49:31Z2011-07-09T22:49:31Zinjected model (singleton) equals null<div><p>Ah - ok, that's it then!</p>
<p>Instead, do:</p>
<p>var playerRobotModel: PlayerRobotModel = new PlayerRobotModel(
new RobotView( 4, 0xFF3399, 4, 30, 290, 20 ) );<br>
injector.injectInto(playerRobotModel);</p>
<p>dispatch( new RobotEvent( RobotEvent.CREATE_ROBOT,
playerRobotModel) );</p>
<p>Injection isn't <em>magic</em> - it's just a smart factory - you
still have to use the injector to fulfil the injections :)</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/85028582011-07-09T23:24:31Z2011-07-09T23:25:31Zinjected model (singleton) equals null<div><p>Yes, that was it :)</p>
<p>Thank you very much for your help Stray, much appreciated.</p></div>patryk