java - Tasks in Spring -


i have application (spring 4 mvc+ jpa + mysql+maven integration example using annotations) , integrating spring hibernate using annotation based configuration; , want create task in order use in controller

having task:

@configurable public class smssendertask implements runnable {      protected static final logger logger = loggerfactory.getlogger(smssendertask.class);      @autowired     smssender smsservice;      private string msg;      private string to;      public smssendertask(string msg, string to) {         super();         this.msg = msg;         this.to = to;     }        @override     public void run() {          try {              smsservice.sendsms(msg, to);          } catch (unsupportedencodingexception e) {             logger.error(e.getmessage());         } catch (clientprotocolexception e) {             logger.error(e.getmessage());         } catch (ioexception e) {             logger.error(e.getmessage());         }     } } 

and other one

public class smssendertaskexecutor {      private taskexecutor taskexecutor;       @autowired      smssender smsservice;       @autowired       public smssendertaskexecutor(taskexecutor taskexecutor) {         this.taskexecutor = taskexecutor;       }       public void sendsms(string msg, string to) {           taskexecutor.execute(new smssendertask (msg, to));      }  } 

--

i put piece of code in controller got nullpointerexception

smssendertask smssendertask = new smssendertask ("ddd", "33473664038"); smssendertask.run(); 

when u use new statement construct task object.u autowired service smsservice hasn't been initialized yet. when u invoke run method, when execution comes smsservice.sendsms(msg, to); , program throws null pointerexception.

u can put smsservice constructor parameter ur code avoid happen.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -