Re: NullPointer for service constructor
It seems like tapestry ioc has @PostInjection ( https://tapestry.apache.org/injection-in-detail.html#InjectioninDetail-Post-InjectionMethods) to receive a callback after the instance is configured.Somewhat similar to the afterPropertiesSet(). On Tue, Apr 8, 2014 at 3:55 PM, Lance Java wrote: > FYI, in spring ioc you work around this issue via implementing > InitializingBean.afterPropertiesSet() > > > http://docs.spring.io/spring/docs/2.5.6/api/org/springframework/beans/factory/InitializingBean.html > > As has been mentioned many times here, constructor injection is superior to > field injection for many reasons >
Re: NullPointer for service constructor
FYI, in spring ioc you work around this issue via implementing InitializingBean.afterPropertiesSet() http://docs.spring.io/spring/docs/2.5.6/api/org/springframework/beans/factory/InitializingBean.html As has been mentioned many times here, constructor injection is superior to field injection for many reasons
Re: NullPointer for service constructor
On Tue, 08 Apr 2014 10:04:20 -0300, Chris Mylonas wrote: Hi t5 aficionados, Hi! Am I correct in following this thread...that it is best to think of @inject as "lazy" inject? (After constructor) No. It's not lazy because the injection is done before the service instance. If so, without knowledge of tapestry internally, sounds like a familiar gotcha like onActivate/setupRender and instantiation/fetching-stuff I'm sorry, but the analogy is completely off. That plastic morphing sounds like a can of worms btw Actually, no, this isn't related to Plastic at all. That NPE would happen in the exact same way in Spring. Again, service classes are *not* transformed by Plastic at all. Instead, it creates proxies that delegate method calls to the service instances. Just Tapestry pages, components, mixins and classes in the base package are transformed. Chasing-those-exceptions is never fun, although I'd prefer to do that than compliance documentation and openssl heartbleed shenanigans! Cant believe it'll be Wednesday soon! Whoosh! :D -- Thiago H. de Paula Figueiredo Tapestry, Java and Hibernate consultant and developer http://machina.com.br - To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org For additional commands, e-mail: users-h...@tapestry.apache.org
Re: NullPointer for service constructor
Hi t5 aficionados, Am I correct in following this thread...that it is best to think of @inject as "lazy" inject? (After constructor) If so, without knowledge of tapestry internally, sounds like a familiar gotcha like onActivate/setupRender and instantiation/fetching-stuff That plastic morphing sounds like a can of worms btw Chasing-those-exceptions is never fun, although I'd prefer to do that than compliance documentation and openssl heartbleed shenanigans! Cant believe it'll be Wednesday soon! Cheers Chris On 08/04/2014 9:07 pm, "Thiago H de Paula Figueiredo" wrote: > On Tue, 08 Apr 2014 05:23:13 -0300, Lance Java > wrote: > > I'm sure Tapestry could do some byte code magic via plastic such that any >> @Inject fields accessed in the constructor were swapped out for getters >> with nice error messages. Sounds very messy / intrusive to me and I don't >> think it should be implemented. >> > > Hi, Lance! > > Hmmm, you're right, that's probably very doable (I'm not sure Plastic > already handles throwing exceptions, and it supports just the bytecode it > needs). You've thought in an angle I haven't though. :) Regarding being > implemented or not, I think this kind of NPE is one that occurs in the > exact same way with ordinary, non-Tapestry-IoC Java, so I agree that I > shouldn't be implemented and that constructor injection is the recommended > one. > > > On 7 Apr 2014 13:54, "Thiago H de Paula Figueiredo" >> wrote: >> >> On Sun, 06 Apr 2014 16:26:47 -0300, John wrote: >>> >>> problem solved, the Logger was of course not yet injected! Duh. I wish >>> the IoC stack dump were more helpful. >>> I'm not sure how (a better error message) that could be done. How could >>> Tapestry-IoC differentiate an exception caused by field injection not >>> done >>> yet from other exceptions? Even if it's just about NPEs, I cannot see how >>> that could be done. >>> >>> -- >>> Thiago H. de Paula Figueiredo >>> Tapestry, Java and Hibernate consultant and developer >>> http://machina.com.br >>> >>> - >>> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org >>> For additional commands, e-mail: users-h...@tapestry.apache.org >>> >>> >>> > > -- > Thiago H. de Paula Figueiredo > Tapestry, Java and Hibernate consultant and developer > http://machina.com.br > > - > To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org > For additional commands, e-mail: users-h...@tapestry.apache.org > >
Re: NullPointer for service constructor
On Tue, 08 Apr 2014 05:23:13 -0300, Lance Java wrote: I'm sure Tapestry could do some byte code magic via plastic such that any @Inject fields accessed in the constructor were swapped out for getters with nice error messages. Sounds very messy / intrusive to me and I don't think it should be implemented. Hi, Lance! Hmmm, you're right, that's probably very doable (I'm not sure Plastic already handles throwing exceptions, and it supports just the bytecode it needs). You've thought in an angle I haven't though. :) Regarding being implemented or not, I think this kind of NPE is one that occurs in the exact same way with ordinary, non-Tapestry-IoC Java, so I agree that I shouldn't be implemented and that constructor injection is the recommended one. On 7 Apr 2014 13:54, "Thiago H de Paula Figueiredo" wrote: On Sun, 06 Apr 2014 16:26:47 -0300, John wrote: problem solved, the Logger was of course not yet injected! Duh. I wish the IoC stack dump were more helpful. I'm not sure how (a better error message) that could be done. How could Tapestry-IoC differentiate an exception caused by field injection not done yet from other exceptions? Even if it's just about NPEs, I cannot see how that could be done. -- Thiago H. de Paula Figueiredo Tapestry, Java and Hibernate consultant and developer http://machina.com.br - To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org For additional commands, e-mail: users-h...@tapestry.apache.org -- Thiago H. de Paula Figueiredo Tapestry, Java and Hibernate consultant and developer http://machina.com.br - To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org For additional commands, e-mail: users-h...@tapestry.apache.org
Re: NullPointer for service constructor
I'm sure Tapestry could do some byte code magic via plastic such that any @Inject fields accessed in the constructor were swapped out for getters with nice error messages. Sounds very messy / intrusive to me and I don't think it should be implemented. On 7 Apr 2014 13:54, "Thiago H de Paula Figueiredo" wrote: > On Sun, 06 Apr 2014 16:26:47 -0300, John wrote: > > problem solved, the Logger was of course not yet injected! Duh. I wish >> the IoC stack dump were more helpful. >> > > I'm not sure how (a better error message) that could be done. How could > Tapestry-IoC differentiate an exception caused by field injection not done > yet from other exceptions? Even if it's just about NPEs, I cannot see how > that could be done. > > -- > Thiago H. de Paula Figueiredo > Tapestry, Java and Hibernate consultant and developer > http://machina.com.br > > - > To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org > For additional commands, e-mail: users-h...@tapestry.apache.org > >
Re: NullPointer for service constructor
On Sun, 06 Apr 2014 16:26:47 -0300, John wrote: problem solved, the Logger was of course not yet injected! Duh. I wish the IoC stack dump were more helpful. I'm not sure how (a better error message) that could be done. How could Tapestry-IoC differentiate an exception caused by field injection not done yet from other exceptions? Even if it's just about NPEs, I cannot see how that could be done. -- Thiago H. de Paula Figueiredo Tapestry, Java and Hibernate consultant and developer http://machina.com.br - To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org For additional commands, e-mail: users-h...@tapestry.apache.org
Re: NullPointer for service constructor
Constructor injection has these three distinct advantages over field/setter injection: - always thread safe - fields can be marked final - you don't have to keep references to objects you need during construction Kalle On Sun, Apr 6, 2014 at 1:08 PM, Sanket Sharma wrote: > I battled with the same issue yesterday - injection happens after the > constructor has run! > > > On Sun, Apr 6, 2014 at 9:26 PM, John wrote: > > > problem solved, the Logger was of course not yet injected! Duh. I wish > the > > IoC stack dump were more helpful. > > > > J > > - Original Message - > > From: John > > To: users@tapestry.apache.org > > Sent: Sunday, April 06, 2014 6:35 PM > > Subject: NullPointer for service constructor > > > > > > I'm trying to constuct a service implimentation that receives > references > > to a couple of other services that registers the objects into a super > > class. But I get a null pointer exception. > > > > What am I doing wrong here? > > > > > > public class AppModule { > > > > public static void bind(ServiceBinder binder) { > > // bind low-level services and utilities > > binder.bind(CommonsEmailNotificationSender.class); // > implements > > NotificationSender to send email > > binder.bind(TextMarketerNotificationSender.class); // > implements > > NotificationSender to send sms > > > >... > > // bind facades > >... > > binder.bind(NotificationSenderFacade.class, > > DefaultNotificationSenderFacade.class); // combines the > NotificationSender > > instances above > > > > } > > > > > > > > public interface NotificationSender { > > > > boolean sendNotification(String toUsername, String fromUsername, > > String replyto, String to, String messageText, boolean admin); > > > > boolean sendNotificationLater(String toUsername, String > > fromUsername, String replyto, String to, String messageText, boolean > admin); > > > > > > > > public interface NotificationSenderFacade { > > > > NotificationSender getNotificationSender(String classnme); > > > > boolean sendNotification(String toUsername, String fromUsername, > > Listroutes, String messageText, boolean admin); > > > > boolean sendNotificationLater(String toUsername, String > > fromUsername, Listroutes, String messageText, > boolean > > admin); > > > > > > > > public abstract class AbstractNotificationSenderFacade implements > > NotificationSenderFacade { > > > > /** > >* The log. > >*/ > > @Inject > > private Logger log; > > > > private Map notificationSenders > > = new HashMap(); > > > > // put the notification senders into a map during subclass > > construction > > protected void registerNotificationSender(NotificationSender > > notificationSender) { > > String classname = > notificationSender.getClass().getSimpleName(); > > notificationSenders.put(classname, notificationSender); > > log.info("registered " + classname); > > } > > > > > > > > @EagerLoad > > public class DefaultNotificationSenderFacade extends > > AbstractNotificationSenderFacade { > > > > /** > >* The log. > >*/ > > @Inject > > private Logger log; > > > > /** > >* Create the facade for the given the notification senders. > >* > >* @param emailNotificationSender > >* @param smsNotificationSender > >*/ > > public > > DefaultNotificationSenderFacade(CommonsEmailNotificationSender > > emailNotificationSender, > > TextMarketerNotificationSender smsNotificationSender) { > > super(); > > super.registerNotificationSender(emailNotificationSender); > > super.registerNotificationSender(smsNotificationSender); > > log.debug("registered notification senders"); > > } > > > > @Override > > public boolean sendNotification(String toUsername, String > > fromUsername, List routes, String messageText, > boolean > > admin) { > > boolean success = true; > > for (NotificationRo
Re: NullPointer for service constructor
I battled with the same issue yesterday - injection happens after the constructor has run! On Sun, Apr 6, 2014 at 9:26 PM, John wrote: > problem solved, the Logger was of course not yet injected! Duh. I wish the > IoC stack dump were more helpful. > > J > - Original Message - > From: John > To: users@tapestry.apache.org > Sent: Sunday, April 06, 2014 6:35 PM > Subject: NullPointer for service constructor > > > I'm trying to constuct a service implimentation that receives references > to a couple of other services that registers the objects into a super > class. But I get a null pointer exception. > > What am I doing wrong here? > > > public class AppModule { > > public static void bind(ServiceBinder binder) { > // bind low-level services and utilities > binder.bind(CommonsEmailNotificationSender.class); // implements > NotificationSender to send email > binder.bind(TextMarketerNotificationSender.class); // implements > NotificationSender to send sms > >... > // bind facades >... > binder.bind(NotificationSenderFacade.class, > DefaultNotificationSenderFacade.class); // combines the NotificationSender > instances above > > } > > > > public interface NotificationSender { > > boolean sendNotification(String toUsername, String fromUsername, > String replyto, String to, String messageText, boolean admin); > > boolean sendNotificationLater(String toUsername, String > fromUsername, String replyto, String to, String messageText, boolean admin); > > > > public interface NotificationSenderFacade { > > NotificationSender getNotificationSender(String classnme); > > boolean sendNotification(String toUsername, String fromUsername, > Listroutes, String messageText, boolean admin); > > boolean sendNotificationLater(String toUsername, String > fromUsername, Listroutes, String messageText, boolean > admin); > > > > public abstract class AbstractNotificationSenderFacade implements > NotificationSenderFacade { > > /** >* The log. >*/ > @Inject > private Logger log; > > private Map notificationSenders > = new HashMap(); > > // put the notification senders into a map during subclass > construction > protected void registerNotificationSender(NotificationSender > notificationSender) { > String classname = notificationSender.getClass().getSimpleName(); > notificationSenders.put(classname, notificationSender); > log.info("registered " + classname); > } > > > > @EagerLoad > public class DefaultNotificationSenderFacade extends > AbstractNotificationSenderFacade { > > /** >* The log. >*/ > @Inject > private Logger log; > > /** >* Create the facade for the given the notification senders. >* >* @param emailNotificationSender >* @param smsNotificationSender >*/ > public > DefaultNotificationSenderFacade(CommonsEmailNotificationSender > emailNotificationSender, > TextMarketerNotificationSender smsNotificationSender) { > super(); > super.registerNotificationSender(emailNotificationSender); > super.registerNotificationSender(smsNotificationSender); > log.debug("registered notification senders"); > } > > @Override > public boolean sendNotification(String toUsername, String > fromUsername, List routes, String messageText, boolean > admin) { > boolean success = true; > for (NotificationRouting route : routes) { > if (route.getTo().contains("@")) { > boolean done = > super.getNotificationSender("CommonsEmailNotificationSender") > .sendNotification(toUsername, fromUsername, > route.getReplyto(), route.getTo(), messageText, admin); > > > > AppModule.NotificationSenderFacade Loading class > com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade. > AppModule.NotificationSenderFacade Marking class > com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade to be > (re-)loaded > AppModule.NotificationSenderFacade BEGIN Analyzing > com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade > AppModule.NotificationSenderFacade Marking class > com.quivinco.webapps.tbs.utils.AbstractNotificationSenderFacade to be > (re-)loaded > AppModule.NotificationSenderFacade BEGIN Analyzing > com.quivinco.webapps.tbs.utils.Abstr
Re: NullPointer for service constructor
problem solved, the Logger was of course not yet injected! Duh. I wish the IoC stack dump were more helpful. J - Original Message - From: John To: users@tapestry.apache.org Sent: Sunday, April 06, 2014 6:35 PM Subject: NullPointer for service constructor I'm trying to constuct a service implimentation that receives references to a couple of other services that registers the objects into a super class. But I get a null pointer exception. What am I doing wrong here? public class AppModule { public static void bind(ServiceBinder binder) { // bind low-level services and utilities binder.bind(CommonsEmailNotificationSender.class); // implements NotificationSender to send email binder.bind(TextMarketerNotificationSender.class); // implements NotificationSender to send sms ... // bind facades ... binder.bind(NotificationSenderFacade.class, DefaultNotificationSenderFacade.class); // combines the NotificationSender instances above } public interface NotificationSender { boolean sendNotification(String toUsername, String fromUsername, String replyto, String to, String messageText, boolean admin); boolean sendNotificationLater(String toUsername, String fromUsername, String replyto, String to, String messageText, boolean admin); public interface NotificationSenderFacade { NotificationSender getNotificationSender(String classnme); boolean sendNotification(String toUsername, String fromUsername, Listroutes, String messageText, boolean admin); boolean sendNotificationLater(String toUsername, String fromUsername, Listroutes, String messageText, boolean admin); public abstract class AbstractNotificationSenderFacade implements NotificationSenderFacade { /** * The log. */ @Inject private Logger log; private Map notificationSenders = new HashMap(); // put the notification senders into a map during subclass construction protected void registerNotificationSender(NotificationSender notificationSender) { String classname = notificationSender.getClass().getSimpleName(); notificationSenders.put(classname, notificationSender); log.info("registered " + classname); } @EagerLoad public class DefaultNotificationSenderFacade extends AbstractNotificationSenderFacade { /** * The log. */ @Inject private Logger log; /** * Create the facade for the given the notification senders. * * @param emailNotificationSender * @param smsNotificationSender */ public DefaultNotificationSenderFacade(CommonsEmailNotificationSender emailNotificationSender, TextMarketerNotificationSender smsNotificationSender) { super(); super.registerNotificationSender(emailNotificationSender); super.registerNotificationSender(smsNotificationSender); log.debug("registered notification senders"); } @Override public boolean sendNotification(String toUsername, String fromUsername, List routes, String messageText, boolean admin) { boolean success = true; for (NotificationRouting route : routes) { if (route.getTo().contains("@")) { boolean done = super.getNotificationSender("CommonsEmailNotificationSender") .sendNotification(toUsername, fromUsername, route.getReplyto(), route.getTo(), messageText, admin); AppModule.NotificationSenderFacade Loading class com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade. AppModule.NotificationSenderFacade Marking class com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade to be (re-)loaded AppModule.NotificationSenderFacade BEGIN Analyzing com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade AppModule.NotificationSenderFacade Marking class com.quivinco.webapps.tbs.utils.AbstractNotificationSenderFacade to be (re-)loaded AppModule.NotificationSenderFacade BEGIN Analyzing com.quivinco.webapps.tbs.utils.AbstractNotificationSenderFacade AppModule.NotificationSenderFacade END Analyzing com.quivinco.webapps.tbs.utils.AbstractNotificationSenderFacade AppModule.NotificationSenderFacade END Analyzing com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade AppModule.CommonsEmailNotificationSender Creating non-proxied instance of service CommonsEmailNotificationSender AppModule.CommonsEmailNotificationSender Invoking constructor com.quivinco.webapps.tbs.utils.CommonsEmailNotificationSender() (at CommonsEmailNotificationSender.java:23) via com.quivinco.webapps.tbs.services.AppModule.bind(ServiceBinder) (at AppModule.java:75) (for service 'CommonsEmailNotificationSender') AppModule.TextMarketerNotif
NullPointer for service constructor
I'm trying to constuct a service implimentation that receives references to a couple of other services that registers the objects into a super class. But I get a null pointer exception. What am I doing wrong here? public class AppModule { public static void bind(ServiceBinder binder) { // bind low-level services and utilities binder.bind(CommonsEmailNotificationSender.class); // implements NotificationSender to send email binder.bind(TextMarketerNotificationSender.class); // implements NotificationSender to send sms ... // bind facades ... binder.bind(NotificationSenderFacade.class, DefaultNotificationSenderFacade.class); // combines the NotificationSender instances above } public interface NotificationSender { boolean sendNotification(String toUsername, String fromUsername, String replyto, String to, String messageText, boolean admin); boolean sendNotificationLater(String toUsername, String fromUsername, String replyto, String to, String messageText, boolean admin); public interface NotificationSenderFacade { NotificationSender getNotificationSender(String classnme); boolean sendNotification(String toUsername, String fromUsername, Listroutes, String messageText, boolean admin); boolean sendNotificationLater(String toUsername, String fromUsername, Listroutes, String messageText, boolean admin); public abstract class AbstractNotificationSenderFacade implements NotificationSenderFacade { /** * The log. */ @Inject private Logger log; private Map notificationSenders = new HashMap(); // put the notification senders into a map during subclass construction protected void registerNotificationSender(NotificationSender notificationSender) { String classname = notificationSender.getClass().getSimpleName(); notificationSenders.put(classname, notificationSender); log.info("registered " + classname); } @EagerLoad public class DefaultNotificationSenderFacade extends AbstractNotificationSenderFacade { /** * The log. */ @Inject private Logger log; /** * Create the facade for the given the notification senders. * * @param emailNotificationSender * @param smsNotificationSender */ public DefaultNotificationSenderFacade(CommonsEmailNotificationSender emailNotificationSender, TextMarketerNotificationSender smsNotificationSender) { super(); super.registerNotificationSender(emailNotificationSender); super.registerNotificationSender(smsNotificationSender); log.debug("registered notification senders"); } @Override public boolean sendNotification(String toUsername, String fromUsername, List routes, String messageText, boolean admin) { boolean success = true; for (NotificationRouting route : routes) { if (route.getTo().contains("@")) { boolean done = super.getNotificationSender("CommonsEmailNotificationSender") .sendNotification(toUsername, fromUsername, route.getReplyto(), route.getTo(), messageText, admin); AppModule.NotificationSenderFacade Loading class com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade. AppModule.NotificationSenderFacade Marking class com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade to be (re-)loaded AppModule.NotificationSenderFacade BEGIN Analyzing com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade AppModule.NotificationSenderFacade Marking class com.quivinco.webapps.tbs.utils.AbstractNotificationSenderFacade to be (re-)loaded AppModule.NotificationSenderFacade BEGIN Analyzing com.quivinco.webapps.tbs.utils.AbstractNotificationSenderFacade AppModule.NotificationSenderFacade END Analyzing com.quivinco.webapps.tbs.utils.AbstractNotificationSenderFacade AppModule.NotificationSenderFacade END Analyzing com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade AppModule.CommonsEmailNotificationSender Creating non-proxied instance of service CommonsEmailNotificationSender AppModule.CommonsEmailNotificationSender Invoking constructor com.quivinco.webapps.tbs.utils.CommonsEmailNotificationSender() (at CommonsEmailNotificationSender.java:23) via com.quivinco.webapps.tbs.services.AppModule.bind(ServiceBinder) (at AppModule.java:75) (for service 'CommonsEmailNotificationSender') AppModule.TextMarketerNotificationSender Creating non-proxied instance of service TextMarketerNotificationSender AppModule.TextMarketerNotificationSender Invoking constructor com.quivinco.webapps.tbs.utils.TextMarketerNotificationSender() (at TextMarketerNotificationSender.java:16) via com.quivinco.webapps.tbs.services.AppModule.bind(ServiceBinder) (at AppModule.java:75) (for service 'TextMarketerNotificationSender') AppModule.NotificationSenderFacade Invoking constructor public com.quivinco.webapps.tbs.utils.DefaultN