eventDispatcher inside Actor/MyModel is null

andrew.catch's Avatar

andrew.catch

18 Feb, 2011 08:33 AM

Hey there,

I'm fairly new to Robotlegs. That is to say I've worked on projects that make use of it...but that doesn't mean I'm a guru :)
I'm wondering how to ensure that eventDispatcher inside Actor isn't null.

I'm rebuilding my website, and I'm using Eclipse Galileo with the Flash Builder 4 plugin, on a Mac. When I try to compile, I get the following error:

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

at org.robotlegs.mvcs::Actor/dispatch()[/Users/myName/Desktop/_Projects/siteName/src/org/robotlegs/mvcs/Actor.as:97]
at com.ldlb.model::ScreenProxy/begin()[/Users/myName/Desktop/_Projects/siteName/src/com/ldlb/model/ScreenProxy.as:64]
at com.ldlb.control::BeginCommand/execute()

plus a whole bunch more but it's because the eventDispatcher instance inside the Actor class is null. In my context I've mapped a StartupCommand to ContextEvent.STARTUP:

    override public function startup():void
    {
        trace("ApplicationContext.startup: ");
        //map commands
        commandMap.mapEvent( ContextEvent.STARTUP, StartupCommand, ContextEvent, true );
        //start the app
        dispatchEvent(new ContextEvent(ContextEvent.STARTUP));
    }

And inside the StartupCommand I do the injections...

    override public function execute():void
    {
        super.execute();
        trace("StartupCommand.execute");
        //map command
        commandMap.mapEvent(ApplicationEvent.LOAD_TEXT, LoadTextCommand, ApplicationEvent, true);
        commandMap.mapEvent(ApplicationServiceEvent.LOAD_TEXT, LoadStructureCommand, ApplicationServiceEvent, true);
        commandMap.mapEvent(ApplicationServiceEvent.LOAD_STRUCTURE, SetStructureCommand, ApplicationServiceEvent, true);
        commandMap.mapEvent(ScreenEvent.NEXT, NextCommand, ScreenEvent, false);
        commandMap.mapEvent(ScreenEvent.CHANGE, ChangeCommand, ScreenEvent, false);
        commandMap.mapEvent(ScreenEvent.BEGIN, BeginCommand, ScreenEvent, true);

        //map model
        injector.mapValue(ScreenProxy, new ScreenProxy());
        //map view
        mediatorMap.mapView(Application, ApplicationMediator);
        mediatorMap.createMediator(contextView);
        //map service
        injector.mapSingletonOf( ApplicationService, ApplicationService );

        //chain command
        dispatch( new ApplicationEvent( ApplicationEvent.LOAD_TEXT));
    }
}

I've tried using some of the other injector methods (mapValue, instantiate) to try and "un-null" eventDispatcher inside my model. There was another issue on here that was similar. I've tried changing the order of injection inside StartupCommand... basically I'm flummoxed. The frustrating thing is at work I use FlashDevelop on a PC and at home I'm on a Mac with Eclipse. It works fine at work. No errors or null references.

Has anyone had any similar errors? Please be gentle... I'm sorry if it's obvious :)

  1. Support Staff 1 Posted by Stray on 18 Feb, 2011 09:14 AM

    Stray's Avatar

    Hi Andrew,

    the problem is how you're instantiating your ScreenProxy.

    you're just doing new ScreenProxy(); when you do this:

        injector.mapValue(ScreenProxy, new ScreenProxy());
    

    The error happens because you need to use the injector to create that object, as it needs the Actor [Inject] dependencies to be fulfilled (the eventDispatcher).

    If you just want to use one ScreenProxy throughout, then just do:

    injector.mapSingleton(ScreenProxy);
    

    This has the side effect of lazy instantiation, so if you definitely need your ScreenProxy to be created straight away then do this:

    injector.mapSingleton(ScreenProxy);
    var disposableInstance:ScreenProxy = injector.getInstance(ScreenProxy);
    

    If you definitely want to use mapValue then you can do this:

    var screenProxy:ScreenProxy = injector.instantiate(ScreenProxy); // creates a new instance each time
        injector.mapValue(ScreenProxy, screenProxy);
    

    And finally... if you ever do need to create a value using new Thing() you can use the following to have injections fulfilled after the fact:

        var screenProxy:ScreenProxy = new ScreenProxy();
        injector.injectInto(screenProxy);
    

    You can check out IInjector for the details - but the first fix I suggested should do you fine.

    And don't be worried about bringing 'obvious' errors. Yesterday I forgot my [Inject] tag on a variable in a Command and scratched my head for a good 20 minutes!

    I hope that sorts it out for you,

    Stray

  2. Support Staff 2 Posted by Till Schneidere... on 18 Feb, 2011 09:17 AM

    Till Schneidereit's Avatar

    Hi Andrew,

    any chance you're using the Robotlegs and Swiftsuspenders sources
    instead of the SWCs in your new project?

    If so, you need to add some arguments to the MXMLC invocation. For
    details, see the (Swiftsuspenders
    README)[https://github.com/tschneidereit/SwiftSuspenders/].

    If that's not it, then I don't immediately know what's going on, either.

    cheers,
    till

  3. Support Staff 3 Posted by Stray on 18 Feb, 2011 09:21 AM

    Stray's Avatar

    Hi Andrew / Till,

    It looks to me like the ScreenProxy is getting injected into the Command - the null error is only coming up when you hit the dispatch in Actor and it looks for the eventDispatcher that should have been injected...

    So I think your MXMLC 'keep metadata' stuff is probably fine.

    Stray

  4. Support Staff 4 Posted by Till Schneidere... on 18 Feb, 2011 09:25 AM

    Till Schneidereit's Avatar

    D'oh! Sorry Andrew: Stray is entirely right - I just didn't look closely enough.

    Thanks Stray!

  5. 5 Posted by andrew.catch on 24 Feb, 2011 10:36 PM

    andrew.catch's Avatar

    Hey so sorry for the late response guys!

    That was extremely helpful. Stray your fixes sorted it straight out. Legendary!

    Thanks so much for your comments, what a great forum :)

    / Andrew

  6. andrew.catch closed this discussion on 24 Feb, 2011 10:36 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