You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ståle Undheim <me...@gmail.com> on 2008/05/05 13:02:23 UTC
Final fields not getting inited?
I have just started using Wicket, and I use Guice for bindings.
However, I got into a situation where private final fields initialized
directly would be null.
Here is the code in my WebApplication constructor:
addComponentInstantiationListener(new IComponentInstantiationListener() {
private final Injector injector = Guice.createInjector(new
Module());
public void onInstantiation(Component component) {
injector.injectMembers(component);
}
});
I prefered this to the wicket guice plugin, that seems to do the above
steps manually by reimplementing Guice logic.
Here is my simple Page body:
private final Label message = new Label("message", "Text");
private Service service;
@Inject
public void init(Service service) {
this.service = service;
add(message);
}
I get a NullPointerException on the add, because message is null.
According to my understanding of Java, I can`t even see how this is
possible. The default constructor should be called regardless, and
thus message would get set.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Final fields not getting inited?
Posted by Johan Compagner <jc...@gmail.com>.
>
> Ah, that explains it. I was under the assumption objects where created
> using Class.forName().newInstance() or some such, then
> instantiationListeners where run on them. Better put @Inject at the
> field level for Guice
>
just curious.. how did you get to that assumption?
If you do in your code:
Label label = new Label()
??
wicket doesnt have factories, except for a bookmarkable page.
Re: Final fields not getting inited?
Posted by Ståle Undheim <me...@gmail.com>.
On Mon, May 5, 2008 at 2:12 PM, lars vonk <la...@gmail.com> wrote:
> This is constructor ordering issue. When constructing an object in Java, at
> first the superclass constructor is executed and the object's own
> constructor. Since the ComponentInstantionListener is executed by the *
> superclass* of your Page, which will call your init method (annotated by
> @Inject), the constructor and field initialization of Page itself isn't
> executed yet. That's why the Label message is still null.
>
Ah, that explains it. I was under the assumption objects where created
using Class.forName().newInstance() or some such, then
instantiationListeners where run on them. Better put @Inject at the
field level for Guice
> On Mon, May 5, 2008 at 1:02 PM, Ståle Undheim <me...@gmail.com> wrote:
>
> > I have just started using Wicket, and I use Guice for bindings.
> > However, I got into a situation where private final fields initialized
> > directly would be null.
> >
> > Here is the code in my WebApplication constructor:
> >
> > addComponentInstantiationListener(new IComponentInstantiationListener() {
> > private final Injector injector = Guice.createInjector(new
> > Module());
> >
> > public void onInstantiation(Component component) {
> > injector.injectMembers(component);
> > }
> > });
> >
> > I prefered this to the wicket guice plugin, that seems to do the above
> > steps manually by reimplementing Guice logic.
> >
> > Here is my simple Page body:
> >
> > private final Label message = new Label("message", "Text");
> >
> > private Service service;
> >
> > @Inject
> > public void init(Service service) {
> > this.service = service;
> > add(message);
> > }
> >
> > I get a NullPointerException on the add, because message is null.
> > According to my understanding of Java, I can`t even see how this is
> > possible. The default constructor should be called regardless, and
> > thus message would get set.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Final fields not getting inited?
Posted by lars vonk <la...@gmail.com>.
This is constructor ordering issue. When constructing an object in Java, at
first the superclass constructor is executed and the object's own
constructor. Since the ComponentInstantionListener is executed by the *
superclass* of your Page, which will call your init method (annotated by
@Inject), the constructor and field initialization of Page itself isn't
executed yet. That's why the Label message is still null.
Lars
On Mon, May 5, 2008 at 1:02 PM, Ståle Undheim <me...@gmail.com> wrote:
> I have just started using Wicket, and I use Guice for bindings.
> However, I got into a situation where private final fields initialized
> directly would be null.
>
> Here is the code in my WebApplication constructor:
>
> addComponentInstantiationListener(new IComponentInstantiationListener() {
> private final Injector injector = Guice.createInjector(new
> Module());
>
> public void onInstantiation(Component component) {
> injector.injectMembers(component);
> }
> });
>
> I prefered this to the wicket guice plugin, that seems to do the above
> steps manually by reimplementing Guice logic.
>
> Here is my simple Page body:
>
> private final Label message = new Label("message", "Text");
>
> private Service service;
>
> @Inject
> public void init(Service service) {
> this.service = service;
> add(message);
> }
>
> I get a NullPointerException on the add, because message is null.
> According to my understanding of Java, I can`t even see how this is
> possible. The default constructor should be called regardless, and
> thus message would get set.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>