I recently ran into a problem with DI. I have a handler (in a fragment) that dynamically generates a new window including a few parts. The fragment includes some commands and their handlers that are used in the generated window.

I need this SequencerFileProcessor in another handler in the same package now (in the CanExecute method to be precise), so I thought I simply can inject it in that handler. But that doesn't work and as a result a NullPointerException is thrown.
I also tried injecting it again via ContextInjectionFactory, but I don't have access to the child context which includes needed values (the path).

[EDIT] It works in the parts I create dynamically afterwards in the handler, so the object must be available in general. Why not in the handler?

Is there anything I get wrong about Dependency Injection or why isn't this working?

How do you inject your value into your handler? Via field or method injection?

Handlers are created in another context (there are several posts in this forum about this). At the time the handler is instantiated, the value is not created yet. At least this is my understanding.

So if I'm right you are using field injection in your handler. Which you definitely shouldn't do. If you change this to method injection and inject your value as method parameter to your @CanExecute or @Execute, it should work.

Yes, I am using field injection. I didn't know that method injection works for the @CanExecute annotation as well. I know it works for @Execute. I will give it try later and then give a quick report here whether it worked for me.

That worked. Thanks a lot. That was easy
Is there any information on that topic in a more "prominent" place like tutorials or wiki? As I said I've read in some tutorial that @Execute includes @Inject, but not for @CanExecute. I think there wouldn't be such questions if this was written anywhere. Maybe I didn't see that information.

All declarative method calls (@Execute, @CanExecute, @PersistState,
@Presist, ...) in the end anything invoked through CIF#invoke() can have
any number of arguments.

Tom

On 03.09.13 14:24, Marina Knieling wrote:
> That worked. Thanks a lot. That was easy ;)
> Is there any information on that topic in a more "prominent" place like
> tutorials or wiki? As I said I've read in some tutorial that @Execute
> includes @Inject, but not for @CanExecute. I think there wouldn't be
> such questions if this was written anywhere. Maybe I didn't see that
> information.