You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Christian Helmbold <ch...@yahoo.de> on 2009/02/28 16:49:55 UTC

NullPointerExceptions due to missing Spring constructor injection

A page in my Wicket app has a contructor which should access an injected repository, but the repository is injected not until construction is complete..

public class ArticlePage extends WebPage
{
    @SpringBean
    private static ArticleRepository repository; // initialized after construction!
    private Article article;

    public ArticlePage()
    {
        this(repository.findByName("index")); // NullPointerException here
    }

    public ArticlePage(Article article)
    {
        add(new Label("name", new PropertyModel(article, "name")));
        add(new Label("contentHtml", new PropertyModel(article, "contentHtml")));
        add(new Label("revision", new PropertyModel(article, "revision")));
        add(new Label("revisionDate", new PropertyModel(article, "revisionDate")));
        add(new Label("author", new PropertyModel(article, "author")));
    }

}

This code looks for the default page in a database an passes it to the real constructor. But this is only a footnote because the problem is more general. 

How do I access injected objects within a constructor? When I try to access "repository" I get a NullPointerException because this field is initialized after construction. The reason is that the Spring integration in Wicket does not allow constructor injection.

How can solve this problem? Is Wicket+Spring (or DI in general) really a good combination? It seems again and again improper to me.

Thanks for help!

Christian

 -- 
http://www.groovy-forum.de



      


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: AW: NullPointerExceptions due to missing Spring constructor injection

Posted by jWeekend <jw...@cabouge.com>.
Christian,

It sounds like you're sinking fast into some quicksand you've created in
your mind - partly because you don't trust, Wicket, Spring, the
Wicket-Spring integration or even Java yet! Your question has nothing to do
with Wicket, Spring or Wicket-Spring integration. 

First of all, forget every assumption and deduction in your original post on
this thread (none is correct). Even if you don't have have time to
experiment with Spring DI, have another read of the 
http://cwiki.apache.org/WICKET/spring.html Wicket-Spring Wiki page  and see
the  http://wicketstuff.org/confluence/display/STUFFWIKI/wicket-phonebook
phone book example . Reading up about object initialisation in Java
(especially implicit superclass constructor calls) may clear a few things up
too.

Once you're familiar with standard usage patterns of Spring DI and then
Wicket's Spring integration, I'm sure you'll appreciate how very "proper"
Wicket-Spring integration actually is; bear in mind that this is used in a
lot of production systems, and it "just works", very nicely.

Here's a solution to your problem (I'll assume you chose to have a null
constructor because you need bookmarkable links)  - 


    @SpringBean private ArticleRepository repository;
    public ArticlePage() {this(null);}
    public ArticlePage(Article article){
        article = article==null?repository.findByName("index"):article;
        add(new Label("name", new PropertyModel(article, "name")));
        ...
    } 

Does that make sense?

Regards - Cema
http://jWeekend.com jWeekend 



christian.helmbold wrote:
> 
> Hi Martjin,
> 
> it is not possible to compile the code without static. Without static I
> get the compiler error: "cannot reference repository before supertype
> constructor has been called". But I cannot write 
> public ArticlePage()
> {
>        super(); 
>        this();
> }
> because each of them must be the first statement in the constructor. And I
> cannot inject the reference directly into the constructor because of the
> mentioned restriction of Wickets Spring integration (no constructor
> injection).
> 
> The only way out of this dilemma seems to be not to inject the repository.
> But if I cannot use injected reference within constructors I don't know
> why I should use Dependency Injection (DI). The constructors are very
> important in most classes of Wicket applications. I could use the the
> service locator pattern with it's pros and cons. But I hope there is a
> solution with DI ...
> 
> Regards
> Christian
> 
> -- 
> http://www.groovy-forum.de
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/NullPointerExceptions-due-to-missing-Spring-constructor-injection-tp22263431p22265963.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: NullPointerExceptions due to missing Spring constructor injection - Workaround

Posted by Igor Vaynberg <ig...@gmail.com>.
a) dont start a new thread for the same problem
b) you dont need this InjectorHolder.getInjector().inject(this); since
your repository is no longer abstract.

-igor

On Sat, Feb 28, 2009 at 11:29 AM, Christian Helmbold
<ch...@yahoo.de> wrote:
> I've found a workaround. Not elegant, but it works:
>
> public class ArticlePage extends WebPage
> {
>    @SpringBean
>    private ArticleRepository repository;
>    private Article article;
>
>    public ArticlePage()
>    {
>        InjectorHolder.getInjector().inject(this);
>        construct(repository.findByName("index"));
>    }
>
>    private void construct(Article article)
>    {
>        add(new Label("name", new PropertyModel(article, "name")));
>        //...
>    }
>
> }
>
> It looks a bit better than service locator pattern but not much. Any other suggestions?
>
> Regards
> Christian
>
> --
> http://www.groovy-forum.de
>
>
>
> ----- Ursprüngliche Mail ----
>> Von: Christian Helmbold <ch...@yahoo.de>
>> An: users@wicket.apache.org
>> Gesendet: Samstag, den 28. Februar 2009, 20:14:02 Uhr
>> Betreff: AW: NullPointerExceptions due to missing Spring constructor injection
>>
>> Hi Martjin,
>>
>> it is not possible to compile the code without static. Without static I get the
>> compiler error: "cannot reference repository before supertype constructor has
>> been called". But I cannot write
>> public ArticlePage()
>> {
>>        super();
>>        this();
>> }
>> because each of them must be the first statement in the constructor. And I
>> cannot inject the reference directly into the constructor because of the
>> mentioned restriction of Wickets Spring integration (no constructor injection).
>>
>> The only way out of this dilemma seems to be not to inject the repository. But
>> if I cannot use injected reference within constructors I don't know why I should
>> use Dependency Injection (DI). The constructors are very important in most
>> classes of Wicket applications. I could use the the service locator pattern with
>> it's pros and cons. But I hope there is a solution with DI ...
>>
>> Regards
>> Christian
>>
>> --
>> http://www.groovy-forum.de
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> 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
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: NullPointerExceptions due to missing Spring constructor injection - Workaround

Posted by Adriano dos Santos Fernandes <ad...@gmail.com>.
Christian Helmbold wrote:
> I've found a workaround. Not elegant, but it works:
>
> public class ArticlePage extends WebPage
> {
>     @SpringBean
>     private ArticleRepository repository;
>     private Article article;
>
>     public ArticlePage()
>     {
>         InjectorHolder.getInjector().inject(this);
>         construct(repository.findByName("index"));
>     }
>
>     private void construct(Article article)
>     {
>         add(new Label("name", new PropertyModel(article, "name")));
>         //...
>     }
>
> }
>
> It looks a bit better than service locator pattern but not much. Any other suggestions?
Create a abstract (or not) function on the base class and call it from 
the base (now without arguments) constructor. Override this function to 
return what you want based on "repository".


Adriano


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


AW: NullPointerExceptions due to missing Spring constructor injection - Solution

Posted by Christian Helmbold <ch...@yahoo.de>.
Excuse my bit by bit mailings but I don't want to keep the solution secret. The problem with my first try was the missing implicit call to super() within the constructor because I used this() instead. The line
InjectorHolder.getInjector().inject(this);
is not necessary when I put the code in a separate method. this works:

public class ArticlePage extends WebPage
{
    @SpringBean
    private ArticleRepository repository;
    private Article article;

    public ArticlePage()
    {
        // no need to use InjectorHolder here
        construct(repository.findByName("index"));
    }

    private void construct(Article article)
    {
        add(new Label("name", new PropertyModel(article, "name")));
        // ...
    }

}

Is there something left to be improved?

Dependency Injection seems to be a bit tricky with wicket. Maybe something for the second edition of wicket in action ...

Regards
Christian

-- 
http://www.groovy-forum.de


      


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


NullPointerExceptions due to missing Spring constructor injection - Workaround

Posted by Christian Helmbold <ch...@yahoo.de>.
I've found a workaround. Not elegant, but it works:

public class ArticlePage extends WebPage
{
    @SpringBean
    private ArticleRepository repository;
    private Article article;

    public ArticlePage()
    {
        InjectorHolder.getInjector().inject(this);
        construct(repository.findByName("index"));
    }

    private void construct(Article article)
    {
        add(new Label("name", new PropertyModel(article, "name")));
        //...
    }

}

It looks a bit better than service locator pattern but not much. Any other suggestions?

Regards
Christian

-- 
http://www.groovy-forum.de



----- Ursprüngliche Mail ----
> Von: Christian Helmbold <ch...@yahoo.de>
> An: users@wicket.apache.org
> Gesendet: Samstag, den 28. Februar 2009, 20:14:02 Uhr
> Betreff: AW: NullPointerExceptions due to missing Spring constructor injection
> 
> Hi Martjin,
> 
> it is not possible to compile the code without static. Without static I get the 
> compiler error: "cannot reference repository before supertype constructor has 
> been called". But I cannot write 
> public ArticlePage()
> {
>        super(); 
>        this();
> }
> because each of them must be the first statement in the constructor. And I 
> cannot inject the reference directly into the constructor because of the 
> mentioned restriction of Wickets Spring integration (no constructor injection).
> 
> The only way out of this dilemma seems to be not to inject the repository. But 
> if I cannot use injected reference within constructors I don't know why I should 
> use Dependency Injection (DI). The constructors are very important in most 
> classes of Wicket applications. I could use the the service locator pattern with 
> it's pros and cons. But I hope there is a solution with DI ...
> 
> Regards
> Christian
> 
> -- 
> http://www.groovy-forum.de
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> 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


AW: NullPointerExceptions due to missing Spring constructor injection

Posted by Christian Helmbold <ch...@yahoo.de>.
> factor out an init method that both constructors call.

Thanks for you help. I've found this solution with the approved trial-and-error method during you wrote me.

Regards
Christian



      


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: NullPointerExceptions due to missing Spring constructor injection

Posted by Igor Vaynberg <ig...@gmail.com>.
factor out an init method that both constructors call.

-igor

On Sat, Feb 28, 2009 at 11:14 AM, Christian Helmbold
<ch...@yahoo.de> wrote:
> Hi Martjin,
>
> it is not possible to compile the code without static. Without static I get the compiler error: "cannot reference repository before supertype constructor has been called". But I cannot write
> public ArticlePage()
> {
>       super();
>       this();
> }
> because each of them must be the first statement in the constructor. And I cannot inject the reference directly into the constructor because of the mentioned restriction of Wickets Spring integration (no constructor injection).
>
> The only way out of this dilemma seems to be not to inject the repository. But if I cannot use injected reference within constructors I don't know why I should use Dependency Injection (DI). The constructors are very important in most classes of Wicket applications. I could use the the service locator pattern with it's pros and cons. But I hope there is a solution with DI ...
>
> Regards
> Christian
>
> --
> http://www.groovy-forum.de
>
>
>
>
>
> ---------------------------------------------------------------------
> 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


AW: NullPointerExceptions due to missing Spring constructor injection

Posted by Christian Helmbold <ch...@yahoo.de>.
Hi Martjin,

it is not possible to compile the code without static. Without static I get the compiler error: "cannot reference repository before supertype constructor has been called". But I cannot write 
public ArticlePage()
{
       super(); 
       this();
}
because each of them must be the first statement in the constructor. And I cannot inject the reference directly into the constructor because of the mentioned restriction of Wickets Spring integration (no constructor injection).

The only way out of this dilemma seems to be not to inject the repository. But if I cannot use injected reference within constructors I don't know why I should use Dependency Injection (DI). The constructors are very important in most classes of Wicket applications. I could use the the service locator pattern with it's pros and cons. But I hope there is a solution with DI ...

Regards
Christian

-- 
http://www.groovy-forum.de


      


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: NullPointerExceptions due to missing Spring constructor injection

Posted by James Carman <ja...@carmanconsulting.com>.
Also, it will inject prior to your constructor is called.

On Feb 28, 2009 11:03 AM, "Martijn Reuvers" <ma...@gmail.com>
wrote:

Hi Christian,

I think you should not use use the static modifier with this, instead use:

@SpringBean
private ArticleRepository repos;

This should work fine and you can use it inside your constructor.
Static does not add anything useful in this case, as its already a
singleton in Spring.

Martijn

On Sat, Feb 28, 2009 at 4:49 PM, Christian Helmbold <
christian.helmbold@yahoo.de> wrote: > A page i...

Re: NullPointerExceptions due to missing Spring constructor injection

Posted by Martijn Reuvers <ma...@gmail.com>.
Hi Christian,

I think you should not use use the static modifier with this, instead use:

@SpringBean
private ArticleRepository repos;

This should work fine and you can use it inside your constructor.
Static does not add anything useful in this case, as its already a
singleton in Spring.

Martijn

On Sat, Feb 28, 2009 at 4:49 PM, Christian Helmbold
<ch...@yahoo.de> wrote:
> A page in my Wicket app has a contructor which should access an injected repository, but the repository is injected not until construction is complete..
>
> public class ArticlePage extends WebPage
> {
>    @SpringBean
>    private static ArticleRepository repository; // initialized after construction!
>    private Article article;
>
>    public ArticlePage()
>    {
>        this(repository.findByName("index")); // NullPointerException here
>    }
>
>    public ArticlePage(Article article)
>    {
>        add(new Label("name", new PropertyModel(article, "name")));
>        add(new Label("contentHtml", new PropertyModel(article, "contentHtml")));
>        add(new Label("revision", new PropertyModel(article, "revision")));
>        add(new Label("revisionDate", new PropertyModel(article, "revisionDate")));
>        add(new Label("author", new PropertyModel(article, "author")));
>    }
>
> }
>
> This code looks for the default page in a database an passes it to the real constructor. But this is only a footnote because the problem is more general.
>
> How do I access injected objects within a constructor? When I try to access "repository" I get a NullPointerException because this field is initialized after construction. The reason is that the Spring integration in Wicket does not allow constructor injection.
>
> How can solve this problem? Is Wicket+Spring (or DI in general) really a good combination? It seems again and again improper to me.
>
> Thanks for help!
>
> Christian
>
>  --
> http://www.groovy-forum.de
>
>
>
>
>
>
> ---------------------------------------------------------------------
> 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