Field Injection return null

2,695 views
Skip to first unread message

Konstantin Zolotarev

unread,
Feb 19, 2012, 11:02:50 AM2/19/12
to gwt-pl...@googlegroups.com
Hello everyone.

I have this problem. I'm trying to use field injection. And trying to inject PresenterWidget. Or custom class binded to Singletone. 
But all of this field is null into all class methods.

@Inject DomainsBagPresenter bag;

@Override
  protected void onBind() {
    getView().setExtensions(PageUtil.get().getTlds());
    setInSlot(BAG_SLOT, bag); // bag here is null
}

Daniel Colchete

unread,
Feb 23, 2012, 7:37:55 AM2/23/12
to gwt-pl...@googlegroups.com
Hi Konstantin,

take a look at my video at http://youtu.be/elT5TOwMMzU, you will see that you should be calling setInSlot at onReset or, if you are inside a popup, onReveal.

It should work if you are creating the presenter widgets with the GWTP Eclipse Plugin. Otherwise it could be a problem somewhere else.

Best,
Dani
--
Dani
Cloud3 Tech - http://cloud3.tc/
Twitter: @DaniCloud3 @Cloud3Tech


Konstantin Zolotarev

unread,
Feb 23, 2012, 10:35:16 PM2/23/12
to gwt-pl...@googlegroups.com
I've created presenter using GWTP eclipse plugin and if I Inject it into constructor, it works. But if I inject it as field it's always null.

Fabian Gebert

unread,
Feb 24, 2012, 12:49:43 AM2/24/12
to gwt-pl...@googlegroups.com

Is your field static? Your non-static field will be available after constructor and after on bind.

Konstantin Zolotarev

unread,
Feb 24, 2012, 1:10:01 AM2/24/12
to gwt-pl...@googlegroups.com
No it's no static. 

If i'll mark it as static will it be loaded before onBind() ?

2012/2/24 Fabian Gebert <fab...@gebert-hh.de>

Is your field static? Your non-static field will be available after constructor and after on bind.

On Feb 24, 2012 4:35 AM, "Konstantin Zolotarev" <konstantin...@gmail.com> wrote:
I've created presenter using GWTP eclipse plugin and if I Inject it into constructor, it works. But if I inject it as field it's always null.



--
Regards.
Zolotarev Konstantin 

Christian Goudreau

unread,
Feb 28, 2012, 12:47:46 PM2/28/12
to gwt-pl...@googlegroups.com
Field injection isn't considered as a "Best practices" but here's how you do Field injection:

You have to call in your binder: requestStaticInjection(classThatNeedFieldInjection);

Cheers,
--
Christian Goudreau

Philippe Beaudoin

unread,
Feb 28, 2012, 4:50:33 PM2/28/12
to gwt-pl...@googlegroups.com
What Christian mentions is for static field injection. For non static fields, save yourself trouble and inject them in the constructor. It's more boilerplate but it will save you a lot of trouble. If you really want to inject non-static fields (again: don't!) then you cannot touch them neither in the constructor not in onBind.

Drew Spencer

unread,
Apr 20, 2012, 9:12:43 AM4/20/12
to gwt-pl...@googlegroups.com
Thanks for clearing that up, Philippe. I was about to post asking about field injection as the Guice docs all say it is evil and makes testing difficult/impossible.

In his (excellent) tutorials, Dani uses field injection to get instances of DispatchAsync and PlaceManager. We should be letting GIN & Guice inject these into the constructor, right?

Anyone help me?

Thanks

jon

unread,
Apr 20, 2012, 10:25:04 AM4/20/12
to gwt-pl...@googlegroups.com
Yes, constructor injection is highly preferred over field injection. I suspect Dani used field injection to get it working fast. He did some other stuff as well (for example not using registerHandler in onBind() for handlers) just to demonstrate and get something working quickly.

Drew Spencer

unread,
Apr 20, 2012, 10:29:57 AM4/20/12
to gwt-pl...@googlegroups.com
Cheers, thought so :) I guess showing people all of that constructor injection is a bit much and it's a simple change to add it to the constructor later on.

Hope he makes more videos though. They were a fantastic intro to GWTP. Really great at understanding nested presenters in particular.

Bruce Grant

unread,
Aug 9, 2012, 11:35:25 AM8/9/12
to gwt-pl...@googlegroups.com
I followed Dani's videos and they were extremely helpful in getting a good grip on GWTP. I ran into the issue with field injection and onBind, did a search, and hit this thread. What I am looking for is an example of how to move the DispatchAsync and PlaceManager field injections to the presenters constructor OR at least a snapshot of the end state.

thx

jon

unread,
Aug 15, 2012, 4:27:11 AM8/15/12
to gwt-pl...@googlegroups.com
Example of constructor injection:

private final PlaceManager placeManager;
private final DispatchAsync dispatcher;

@Inject
public MyPresenter(PlaceManager placeManager, DispatchAsync dispatcher)
{
    ...
   
    this.placeManager = placeManager;
    this.dispatcher = dispatcher;
}
Reply all
Reply to author
Forward
0 new messages