injector.mapSingleton(Model); but [inject] public var board:Model; returns a null.
Hello.
In my main context I have a injector.mapSingleton(Model);
. Then when I try to access the model in a command by doing [inject]
public var model:Model;
all I get when I try to access this model is a null.
The model is extending from Actor and the command obviously extends Command.
I get no error messages except for the exception when trying to access that null objects methods.
What have I missed?
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
Support Staff 1 Posted by Joel Hooks on 09 Aug, 2010 06:34 PM
It is hard to tell what you missed without more context ;)
Can you paste in the actual code (the command and context) and specify where in the command it is null.
Support Staff 2 Posted by Shaun Smith on 09 Aug, 2010 06:40 PM
Perhaps it is null in your command's constructor? Dependencies injected via setter/property injection are not available until after the instance has been created. Have a look at: http://wiki.github.com/robotlegs/robotlegs-framework/common-problems (under "Problem: Injected Properties Are Null In Constructor").
3 Posted by davidtynnhammar on 09 Aug, 2010 06:55 PM
` public class MyContext extends Context {
}`
` public class ACommand extends Command {
} `
4 Posted by davidtynnhammar on 09 Aug, 2010 06:57 PM
Err.. Seems like I can't make it format correctly, but I hope you can understand it anyway :). (Thanks for the quick replies!)
Support Staff 5 Posted by Joel Hooks on 09 Aug, 2010 07:00 PM
The property injection isn't available yet. It will be available for the execute() method of the command. To get the injection in the constructor you could:
public function ACommand(e:MEvent, m:Model) {
}
6 Posted by Jason Dias on 09 Aug, 2010 07:01 PM
Your command should overwrite execute, if for some reason you need the model
available in the constructor of the command you need to use [PostContruct]
tag as Shaun described here
http://wiki.github.com/robotlegs/robotlegs-framework/common-problems (under
"Problem: Injected Properties Are Null In Constructor").
On Mon, Aug 9, 2010 at 11:57 AM, davidtynnhammar <
[email blocked]<tender%[email blocked]>
> wrote:
Support Staff 7 Posted by Joel Hooks on 09 Aug, 2010 07:08 PM
There are one or two use cases for PostCostruct, and this likely isn't one of them - it also wouldn't change the unavailability of the property value in the constructor. Using constructor injection is a cleaner solution, so I guess I disagree with Shaun on this one.
8 Posted by Jason Dias on 09 Aug, 2010 07:15 PM
I also agree, in my opinion PostConstruct in a command means the command is doing to much.
Here is an example of one of my commands,
package {
}
9 Posted by davidtynnhammar on 10 Aug, 2010 04:56 AM
´public class TryMoveCommand extends Command {
}´
This still prints out null's.
Edit: And I'm not using the constructor anymore.
Support Staff 10 Posted by Ondina D.F. on 10 Aug, 2010 06:12 AM
Hi David,
maybe the [inject] is just a typo in your post, but if you have it like this in your code as well, that could be the cause for the error in your command.
[Inject] has to be written with a capital I.
Ondina
11 Posted by Nikos on 10 Aug, 2010 10:13 AM
@Jason
Nice code
12 Posted by davidtynnhammar on 11 Aug, 2010 06:22 AM
It was a typo (in my original code that is). How embarrasing.
Thanks guys!
davidtynnhammar closed this discussion on 11 Aug, 2010 06:22 AM.