injected model (singleton) equals null

patryk's Avatar


09 Jul, 2011 06:51 PM

In main context:

injector.mapSingletonOf( IWorldModel, WorldModel );

In some command:
(it works fine)

[Inject] public var worldModel :IWorldModel;

In another model:
( this doesn't work, worldModel == null )

[Inject] public var worldModel :IWorldModel;

What could I be doing wrong?

  1. 1 Posted by Stray on 09 Jul, 2011 06:56 PM

    Stray's Avatar

    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.

    In a nutshell:

    - typo in [Inject]
    - race conditions (the model needing the mapping is asking for it before the mapping has been made)
    - accidental ; after [Inject]



  2. 2 Posted by patryk on 09 Jul, 2011 07:10 PM

    patryk's Avatar

    Thank you for a quick reply Stray.

    No typos, mapping is done before injection...
    I just checked the common problems doc, and I can not find anything wrong in the code.

    public class WorldModel extends Actor implements IWorldModel
    private var robotVector :Vector.<IRobotModel> = new Vector.<IRobotModel>;

    public function addRobot( robotModel:IRobotModel ):void
    robotModel.grid = getGrid( robotModel.robotView );

    robotVector[ robotVector.length ] = robotModel;

    dispatch( new RobotEvent( RobotEvent.ROBOT_CREATED, robotModel ) );

    ------ that RobotEvent is mapped to DisplayRobotCommand:

    public class DisplayRobotCommand extends Command
    public var worldModel :IWorldModel;

    override public function execute():void
    { event.robotModel.robotView ); /// HERE THE INJECTION WORKS

    -------- and then in WorldModel:

    private function onTimer( e:TimerEvent ):void
    for ( var i:int = 0; i < robotVector.length; i++ ) robotVector[ i ].oEF();

    ---------- and inside that robotModel:

    public class PlayerRobotModel implements IRobotModel
    public var worldModel :IWorldModel;

    private var _robotView :RobotView;

    public function oEF():void
    MonsterDebugger.trace( this, worldModel, "worldModel"); ////// AND HERE COMES THE ERROR:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.

    What am I doing something wrong here?

    BTW, What tags can I use to format the code in this post?

  3. 3 Posted by Stray on 09 Jul, 2011 09:02 PM

    Stray's Avatar

    Hi Patryk, the things in common problems, plus the race conditions, are the only things I've ever heard of being problems.

    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 :)


  4. 4 Posted by patryk on 09 Jul, 2011 09:09 PM

    patryk's Avatar

    Thanks a lot Stray.

    How can I get this trace of the order of mapping, etc ?
    I have seen it somewhere on this forum but can't find it.

    BTW: 10pm here too, in Southampton :)

  5. Support Staff 5 Posted by Stray on 09 Jul, 2011 10:40 PM

    Stray's Avatar

    How is PlayerRobotModel being created? You're not doing new PlayerRobotModel() are you?

    Just a thought... the injector has to be used to create any instance requiring injections.

  6. 6 Posted by patryk on 09 Jul, 2011 10:45 PM

    patryk's Avatar

    I am doing this in one of the commands:

    dispatch( new RobotEvent( RobotEvent.CREATE_ROBOT, new PlayerRobotModel( new RobotView( 4, 0xFF3399, 4, 30, 290, 20 ) ) ) );

  7. 7 Posted by Stray on 09 Jul, 2011 10:49 PM

    Stray's Avatar

    Ah - ok, that's it then!

    Instead, do:

    var playerRobotModel: PlayerRobotModel = new PlayerRobotModel( new RobotView( 4, 0xFF3399, 4, 30, 290, 20 ) );

    dispatch( new RobotEvent( RobotEvent.CREATE_ROBOT, playerRobotModel) );

    Injection isn't *magic* - it's just a smart factory - you still have to use the injector to fulfil the injections :)


  8. 8 Posted by patryk on 09 Jul, 2011 11:24 PM

    patryk's Avatar

    Yes, that was it :)

    Thank you very much for your help Stray, much appreciated.

  9. Ondina D.F. closed this discussion on 01 Nov, 2011 04:13 PM.

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

Keyboard shortcuts


? 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