11 Replies Latest reply on May 4, 2011 9:49 AM by marx3

    Logger (@Logger) is always null (NPE)

    lrpieri

      I've created a new simple project using JBoss Tools for test and use the New > Seam Generate Entities for a simple table called 'pessoa'.
      What I want is create PessoaDAO class with a list() method to be called by PessoaHome. At method pessoaDAOImpl.list() when is called log.info('xyz') i got a NPE.
      What is necessary to I can inject the Logger?


      Enviroment:
      Eclipse: eclipse-jee-galileo-SR2-linux-gtk
      jBoss Tools: 1.3
      jBoss AS: jboss-5.1.0.GA
      jBoss Seam: jboss-seam-2.2.0.GA


      The relevant code are:


      PessoaHome.java


      @Name("pessoaHome")
      public class PessoaHome extends EntityHome\<Pessoa\> {
      ...
              public List\<Pessoa\> getList(){
                      PessoaDAOImpl pessoaDAOImpl \= new PessoaDAOImpl();
                      return pessoaDAOImpl.list();
              }
      }



      PessoaDAO.java




      public interface PessoaDAO {
              public abstract List\<Pessoa\> list();
      }



      PessoaDAOImpl.java


      package org.domain.pessoa.session;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import org.domain.pessoa.entity.Pessoa;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.framework.EntityQuery;
      import org.jboss.seam.log.Log;
      
      @Name("pessoaDAO")
      public class PessoaDAOImpl extends EntityQuery\<Pessoa\> {
      
              @Logger private Log log;
      
              public List\<Pessoa\> list(){
                      log.info("Entrou em list(");//Here the log is null
                      return new ArrayList\<Pessoa\>();
              }
      }



      Does anybody have an idea what is wrong?

        • 1. Re: Logger (@Logger) is always null (NPE)
          cjalmeida

          Try making log variable visibility default, protected or public.

          • 2. Re: Logger (@Logger) is always null (NPE)
            lrpieri

            Hi Cloves,


            The problem persist.
            I think there are any generic injection problem in my tests or in the projects generated by jBoss Tools.


            Do you use jBoss Tools to generate your projects?


            Thx.


            Pieri

            • 3. Re: Logger (@Logger) is always null (NPE)
              lrpieri

              Hi All,


              I've tried more two ways that work but they aren't what I'm looking for.


              1st


              on the view home.xhtml



              ...
                  <h:form >
                          <h:commandButton value="Login" action="#{pessoaDAO.list()}"/>
                  </h:form>
              ...



              PessoaDAO.java


              @Name("pessoaDAO")
              public class PessoaDAOImpl{
                   @Logger Log log;
                   
                   public List<Pessoa> list(){
                        log.info("Entrou em list()");
                        return new ArrayList<Pessoa>();
                   }
                   
              }



              The 1st works fine.


              2nd


              on the view home.xhtml


              ...
                  <h:form >
                          <h:commandButton value="Login" action="#{pessoaHome.getList()}\"/>
                  </h:form>
              ...




              PessoaHome.java


              @Name("pessoaHome")
              public class PessoaHome extends EntityHome<Pessoa> {
              ...
                   @Logger private Log log;
                   @In (create=true) private PessoaDAOImpl pessoaDAO;
                   public List<Pessoa> getList(){
                        System.out.println("PessoaDAO: " + pessoaDAO);
                        return pessoaDAO.list();
                   }
              }



              The 2nd works fine


              But...
              If I change PessoaHome for:



              @Name("pessoaHome")
              public class PessoaHome extends EntityHome<Pessoa> {
              ...
                   @Logger private Log 
                   public List<Pessoa> getList(){
                              PessoaDaoImp pessoaDAO = new PessoaDAOImpl();
                        System.out.println("PessoaDAO: " + pessoaDAO);
                        return pessoaDAO.list();
                   }
              }



              I'll get a NPE on log.info("Entrou em list()");


              @Name("pessoaDAO")
              public class PessoaDAOImpl{
                   @Logger Log log;
                   
                   public List<Pessoa> list(){
                        log.info("Entrou em list()");
                        return new ArrayList<Pessoa>();
                   }
                   
              }



              What is wrong here?
              Isn't enough for the PessoaDAOImpl have @Name to @Logger works?
              Does it have to be used in a injection to work?
              Or have to be used as a seam component?



              Thx for all.

              • 4. Re: Logger (@Logger) is always null (NPE)
                pmuir

                Please post in the Seam Users forum, this forum is for Weld only.

                • 5. Re: Logger (@Logger) is always null (NPE)
                  hbender

                  If you create the class directly with new operator, all the magic behind the annotations is gone. You have to let the seam framework do the job, like:


                  Component.getInstance( 'pessoaDAO' );


                  Heri

                  • 6. Re: Logger (@Logger) is always null (NPE)
                    cjalmeida

                    As more people start using Weld, since the J2EE spec gives big publicity, this question will arise more often.


                    Maybe it should be the first line of the documentation, in big bold letters:



                    DON'T PANIC! And don't use the 'new' keyword.
                    • 7. Re: Logger (@Logger) is always null (NPE)

                      Hi,


                      I had the same issue (null pointer exception at log) when I switched my SLSB to POJO.


                      What solved the problem was simply this:




                      org.jboss.logging.Logger log = new org.jboss.logging.Logger.getLogger(YourClass.class);



                      Ofcourse my object is still a seam managed object (with @Name annotation)









                      • 8. Re: Logger (@Logger) is always null (NPE)
                        isonisak

                        I have this same error with Seam 2.2.1.CR2, JBoss 6 Final.


                        Application is generated by seam-gen and works fine.


                        When loggging is added comes always NPE no matter how logging is instantiated.


                        import org.jboss.seam.annotations.Logger;


                        @Logger public Log log;   
                        log.debug(test seam logging);   --  NPE


                        @Logger private Log log;   
                        log.debug(test seam logging);   -- NPE


                        @Logger Log log;   
                        log.debug(test seam logging);   -- NPE


                        In JBoss AS 6 commmon lib is log4.jar and server start advertises this lib.
                        I copied this jar to ../default/lib.


                        Result is the same.  Maybe jar must be in applications libraries in this AS version ?

                        • 9. Re: Logger (@Logger) is always null (NPE)
                          isonisak

                          Hai


                          This problem is not Seam version or platfrom spesific.


                          Probably the cause is that appropriate class where query is started
                          is not injected by Seam.
                          This is bad architecture for new Seam users and I suggest that
                          seam-gen is changed so, that server methods are called from UI so, that
                          @Logger  works.


                          This requires that methods are instantiated from UI with #{bean.method}
                          notation that causes, that Seam catches the call and injects Logger etc.


                          Am I right ?

                          • 10. Re: Logger (@Logger) is always null (NPE)
                            jorgwel

                            This assumption really helped me. I have the same NullPointerException with my loggers when I wasn't invoking methods from the UI. So Instead of keep using @In, I changed every injection point to something like this:




                            (ResizeImageManager)Component.getInstance("resizeImageManager");





                            'Using the injection progamatically' , now all the magic in seam is working.


                            Thanks for this post.

                            • 11. Re: Logger (@Logger) is always null (NPE)
                              marx3

                              Using injected logger is bad idea. For example such logger doesn't work in static method or in costructor.