On Mon, May 5, 2008 at 2:12 PM, lars vonk <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]