2 Replies Latest reply on Feb 15, 2013 5:22 AM by feder

    Injecting a Logger within a Java SE application results in a NullPointerException

    feder Newbie

      Hi,

       

      I'm working on a new open source Java SE library. Presumably I'm missing a step about injection of a Log4J instance, since I always receive a NullpointerException when I wish to log. E.g. invoking logger.warning("foo").

       

      It goes without saying, that I have added Weld SE to may Maven dependencies

           {code:xml}<dependency>

                  <groupId>org.jboss.weld</groupId>

                  <artifactId>weld-se</artifactId>

                  <version>1.0.1-Final</version>

              </dependency>{code}

       

      I also added the beans.xml to /src/main/resources/META-INF/

       

      I added the producer method.

      {code}

      package com.melinxa.logic.factories;

       

      import javax.enterprise.inject.Produces;

      import javax.enterprise.inject.spi.InjectionPoint;

      import org.apache.log4j.Logger;

       

      public class LogFactory {

       

          @Produces

          public Logger createLogger(InjectionPoint injectionPoint) {

              return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());

          }

      }

      {code}

       

      Finally, I wish to inject a Logger with each of my Java files. But it returns a NullPointerException.

      {code}@Inject Logger logger{code}

       

       

      What is it that I'm missing? Why is the logger variable not instantiated?

       

      Again, it all works fine with Java EE (e.g. JBOSS AS 7) but it fails with a simple Java SE application.

       

      thanks a lot for any hints.

       

      Feder