You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by "Stammler, Daniel" <da...@sap.com> on 2012/07/26 17:24:44 UTC

Nullpointer with Inject in Timer run()

Hello,

we have some problems with the periodical execution of a function. We're using tapestry version 5.2.6. Since we don't want to update the tapestry version right now, we can not use the tapestry timer and have to use the Java Timer.

Our timertask looks like:

public class OurTimerTask extends TimerTask {
@Inject
OurService ourService;

        @Override
        public void run() {
                ourService.execute();

        }
}

And the service that starts the timer:

Timer timer = new Timer("MyTimer");
OurTimerTask timerTask = new OurTimerTask();

                // its the date of the first execution. It should be in the night of the
                // current day

timer.schedule(timerTask, 100,100);

The problem now is that the Inject in our TimerTask doesn't work. We tried this in many ways but the object ourService is always null if the run() function gets called. Without the Timer it works. Is it even possible to Inject a service for the timer? We hope that anyone can help us.

Kind regards
Daniel


Re: Nullpointer with Inject in Timer run()

Posted by Cezary Biernacki <ce...@gmail.com>.
Hi Daniel,
Tapestry does not process objects created using new. You can however try to
use ObjectLocator service. For example:

public class MyPage {

 @Inject private ObjectLocator locator;



.... inside some method ...

Timer timer = new Timer("MyTimer");
OurTimerTask timerTask = locator.autobuild(OurTimerTask.class);
timer.schedule(timerTask, 100,100);

}

Alternatively, you can implement a factory service that would just create
OurTimerTask instances and provide them with a reference to OurService.

Cezary
On Thu, Jul 26, 2012 at 5:24 PM, Stammler, Daniel
<da...@sap.com>wrote:

> Hello,
>
> we have some problems with the periodical execution of a function. We're
> using tapestry version 5.2.6. Since we don't want to update the tapestry
> version right now, we can not use the tapestry timer and have to use the
> Java Timer.
>
> Our timertask looks like:
>
> public class OurTimerTask extends TimerTask {
> @Inject
> OurService ourService;
>
>         @Override
>         public void run() {
>                 ourService.execute();
>
>         }
> }
>
> And the service that starts the timer:
>
> Timer timer = new Timer("MyTimer");
> OurTimerTask timerTask = new OurTimerTask();
>
>                 // its the date of the first execution. It should be in
> the night of the
>                 // current day
>
> timer.schedule(timerTask, 100,100);
>
> The problem now is that the Inject in our TimerTask doesn't work. We tried
> this in many ways but the object ourService is always null if the run()
> function gets called. Without the Timer it works. Is it even possible to
> Inject a service for the timer? We hope that anyone can help us.
>
> Kind regards
> Daniel
>
>

Re: Nullpointer with Inject in Timer run()

Posted by Lenny Primak <lp...@hope.nyc.ny.us>.
You can only @Inject tapestry services into other tapestry controlled objects. Your timer task isn't one. 
You will need to pass the service into your timer object's constrictor or some other method.  



On Jul 26, 2012, at 11:24 AM, "Stammler, Daniel" <da...@sap.com> wrote:

> Hello,
> 
> we have some problems with the periodical execution of a function. We're using tapestry version 5.2.6. Since we don't want to update the tapestry version right now, we can not use the tapestry timer and have to use the Java Timer.
> 
> Our timertask looks like:
> 
> public class OurTimerTask extends TimerTask {
> @Inject
> OurService ourService;
> 
>        @Override
>        public void run() {
>                ourService.execute();
> 
>        }
> }
> 
> And the service that starts the timer:
> 
> Timer timer = new Timer("MyTimer");
> OurTimerTask timerTask = new OurTimerTask();
> 
>                // its the date of the first execution. It should be in the night of the
>                // current day
> 
> timer.schedule(timerTask, 100,100);
> 
> The problem now is that the Inject in our TimerTask doesn't work. We tried this in many ways but the object ourService is always null if the run() function gets called. Without the Timer it works. Is it even possible to Inject a service for the timer? We hope that anyone can help us.
> 
> Kind regards
> Daniel
> 

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