2 Replies Latest reply on Nov 27, 2015 12:16 AM by vipinlal.tg

    @Inject instance is null on start up servlet

    vipinlal.tg

      Hello All,

       

       

      am new in CDI, (Container Dependency Injection).

      My problem is and injecting one bean class and using that injected bean class instance used in servlet constructor.

      The servlet is call on start up  of server, on that time am getting null at the point of injected bean instance.

       

      How can i resolve this issue

       

      Am highlighting the Injecting bean part with red color

       

       

       

       

      import java.io.IOException;

      import java.util.Date;

      import java.util.Timer;

      import java.util.concurrent.TimeUnit;

       

      import javax.inject.Inject;

      import javax.servlet.ServletException;

      import javax.servlet.annotation.WebServlet;

      import javax.servlet.http.HttpServlet;

      import javax.servlet.http.HttpServletRequest;

      import javax.servlet.http.HttpServletResponse;

       

      import com.beo.udi.fda.access.MasAccess;

      import com.beo.udi.util.ApplicationFilePath;

      import com.beo.udi.util.AutoFileChecker;

       

      /**

      * Servlet implementation class InitialServlet

      */

      @WebServlet("/InitialServlet")

      public class InitialServlet extends HttpServlet {

          private static final long serialVersionUID = 1L;

          int MIN_TIME= 1000 * 60;

          /**

           * @see HttpServlet#HttpServlet()

           */

         

         @Inject MasAccess mas;

          public InitialServlet() {

              super();

              new ApplicationFilePath();

              Timer time = new Timer();

              AutoFileChecker task = new AutoFileChecker();

              time.schedule(task, new Date(), TimeUnit.MINUTES.toMinutes(MIN_TIME));

             

              mas.checkCount(); //getting null pointer exception here

          }

       

          /**

           * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

           */

          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

              // TODO Auto-generated method stub

              response.getWriter().append("Served at: ").append(request.getContextPath());

          }

       

          /**

           * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

           */

          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

              // TODO Auto-generated method stub

              doGet(request, response);

          }

       

      }

       

       

      Thanks & Regards

      Vipin

        • 1. Re: @Inject instance is null on start up servlet
          jaysensharma

          Let the servlet initialized properly.

           

          Servlets are initialized by the web container. servlets is not initialized unlike other java class. Constructor are used to initialize an explicitly created object while servlet use a different method 'init()' for their initialization.

           

          Servlet Constructor is not the right place to put some initialization code rather put it  inside the   "init(ServletConfig config)" life-cycle method of Servlet.

          See:  https://docs.oracle.com/javaee/6/api/javax/servlet/GenericServlet.html#init(javax.servlet.ServletConfig)

           

           

          Example:

           

          @Inject MasAccess mas;
              public InitialServlet() {
                  super();
                  new ApplicationFilePath();
                  Timer time = new Timer();
                  AutoFileChecker task = new AutoFileChecker();
                  time.schedule(task, new Date(), TimeUnit.MINUTES.toMinutes(MIN_TIME));
                  //  //////////      REMOVED   ->   mas.checkCount(); //getting null pointer exception here
              }
          
              public void init(ServletConfig servletConfig) {
                    System.out.println("### **** [init(ServletConfig servletConfig)]: mas.checkCount() invoked Here");
                    mas.checkCount();
              }
          
            /*
              Alternate approach by using @PostConstruct annotation as following:
              @javax.annotation.PostConstruct
              public void hello() {
                    System.out.println("### **** [hello] PostConstruct() invoked.");
                   mas.checkCount();
              }
             */
          
          
          
          

           

           

          Or alternatively you may try "javax.annotation.PostConstruct" as demonstrated above.

          https://docs.oracle.com/javaee/5/api/javax/annotation/PostConstruct.html

          • 2. Re: @Inject instance is null on start up servlet
            vipinlal.tg

            Hello ,

             

            My requirement is that, on server start up i need to read some file from specific location also i need to check every 1 min to for any file came.

            for that am created one TimerTask class AutoFileChecker and it will call on start of the server from servlet.

             

            The issue is am using CDI concept for calling between beans. and these beans are also using for this read file handling,

            on startup server am creating instance of requestscoped beans using @inject but it is getting null on start up

             

            Sample code below

            class InitialServlet.java calling on start up

             

            public class InitialServlet extends HttpServlet {

                private static final long serialVersionUID = 1L;

                int MIN_TIME= 1000 * 60;

                /**

                 * @see HttpServlet#HttpServlet()

                 */

                public InitialServlet() {

                    super();

                }

                AutoFileChecker task;

                @Override

                public void init(ServletConfig config) throws ServletException {

                    new ApplicationFilePath();

                    Timer time = new Timer();

                    //AutoFileChecker task = new AutoFileChecker();

                    time.schedule(task, new Date(), TimeUnit.MINUTES.toMinutes(MIN_TIME));

                }

               

                /**

                 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

                 */

                protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                    // TODO Auto-generated method stub

                    response.getWriter().append("Served at: ").append(request.getContextPath());

                }

             

                /**

                 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

                 */

                protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                    // TODO Auto-generated method stub

                    doGet(request, response);

                }

                @Inject

                public void setTask(AutoFileChecker task) {

                    this.task = task;

                }

             

            }

             

            AutoFileChecker.java

             

            public class AutoFileChecker extends TimerTask implements ApplicationFilePathInterface{

             

                @Override

                public void run() {

                    readReplayMessageFiles(ApplicationFilePath.getApplicationPath().getFolderPath(udi_replay_message_location),

                            ApplicationFilePath.getApplicationPath().getFolderPath(udi_replay_message_done_location));

                    new AutoSend().doAutoSend();

                }

            }

             

             

            public class AutoSend implements ApplicationFilePathInterface{

            @inject private MasAccess accesss;

                public AutoSend() {

                    // TODO Auto-generated constructor stub

                }

               

                public void doAutoSend() {

                   this.accesss.checkCount();//getting null pointer here

                 }

            }

             

            @RequestScoped

            public class MasAccess {

                 //some code

            }